Articles in this section
Category / Section

How can I move the entire row (all columns) up and down within a GridDataBoundGrid?

5 mins read

 

In a 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 = CType(cm.List(row1), DataRowView)

Dim val1 As Integer = CInt(drv1.Row("sortKey"))

Dim drv2 As DataRowView = CType(cm.List(row2), DataRowView)

Dim val2 As Integer = CInt(drv2.Row("sortKey"))

drv1.Row("sortKey") = val2

drv2.Row("sortKey") = val1

End If

End Sub

Sample:

http://websamples.syncfusion.com/samples/kb/grid.windows/GDBGMoveRows/main.htm

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