We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridDataBoundGrid(Classic) |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014

 

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

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon