Articles in this section
Category / Section

How to move the entire row up and down within a WinForms GridDataBoundGrid?

6 mins read

In a WinForms GridDataBoundGrid, the grid just displays the rows as they are presented to it by the underlying DataSource. So, if you want to move the position of the rows currently, you have to do so by moving them in the underlying DataSource.

 

One way of doing it is to add an additional column to the DataTable that holds a SortKey that reflects where you want the rows to be. You don't display this column (you can hide using GridBoundColumn for the columns you want to see, or you can just hide it in the grid as the sample does). When you initially set the DataSource to the grid, you sort the DataTable on this column using its DefaultView.Sort property. Then when you want to move rows, all you have to do is to move the SortKey values and the rows will rearrange themselves (as they must maintain the sort order).


C#

void Swap(int row1, int row2)
{
     CurrencyManager cm = (CurrencyManager)this.BindingContext[this.gridDataBoundGrid1.DataSource,
         this.gridDataBoundGrid1.DataMember];

     if (row1 < cm.Count && row2 < cm.Count && row1 > -1 && row2 > -1)
      {
         DataRowView drv1 = (DataRowView)cm.List[row1];
         int val1 = (int)drv1.Row["sortKey"];
         DataRowView drv2 = (DataRowView)cm.List[row2];
         int val2 = (int)drv2.Row["sortKey"];
         drv1.Row["sortKey"] = val2;
         drv2.Row["sortKey"] = val1;
      }
  }


VB

Private Sub Swap(ByVal row1 As Integer, ByVal row2 As Integer)

  Dim cm As CurrencyManager = CType(Me.BindingContext(Me.gridDataBoundGrid1.DataSource,    Me.gridDataBoundGrid1.DataMember), CurrencyManager)

	If row1 < cm.Count AndAlso row2 < cm.Count AndAlso row1 > -1 AndAlso row2 > -1 Then

	    Dim drv1 As DataRowView = DirectCast(cm.List(row1), DataRowView)
	    Dim val1 As Integer = DirectCast(drv1.Row("sortKey"), Integer)
	    Dim drv2 As DataRowView = DirectCast(cm.List(row2), DataRowView)
	    Dim val2 As Integer = DirectCast(drv2.Row("sortKey"), Integer)
	    drv1.Row("sortKey") = val2
	    drv2.Row("sortKey") = val1
       End If
End Sub


Conclusion

I hope you enjoyed learning about moving the entire row up and down within a WinForms GridDataBoundGrid.

You can refer to our WinForms GridDataBoundGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied