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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to drag and drop rows or moving rows by single click on the row header in GridDataBoundGrid ?

Platform: WinForms |
Control: GridDataBoundGrid(Classic)

 

In a GridDataBoundGrid, the grid just displays the rows as they are presented to it by the underlying datasource. You cannot easily re-order rows (and maintain this order) in a sorted DataView, as the DataView wants to immediately re-sort things as soon as some change messes up the sort order. 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 add an extra DataColumn to your DataTable that holds a sortKey that reflects where you want the rows to be and use that column to always maintain a particular sort order. 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).

Once you have some kind of support for maintaining arbitrary row orders in GridDataBoundGrid,the you can handle the mouse events to catch the MouseDown header and to manage the UI part of moving the rows.

The attached sample shows how you can implement a row moving routine that allows you to mouse down on the row header, and then just drag it (i.e By single click on the row header, and then to drag it). It uses cursors to give feedback during the move.

Steps for drag and drop a row:

1.Move the mouse pointer to any row header

2.Single Click on the row header (now you see a OLE Drag and Drop mouse curosr)

3.Move the row to the target

C#

public class DragRowHeaderGrid : GridDataBoundGrid

{

public DragRowHeaderGrid()

{

this.AllowDrop= false;

this.AllowSelection &= ~GridSelectionFlags.Row;

this.SortBehavior = GridSortBehavior.SingleClick;

}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)

{

if(inRowMove)

{

if(targetRow > 0 && targetCol == 0 && sourceRow > 0)

{

int dir = sourceRow > targetRow ? -1 : 1;

int gridRow = this.CurrentCell.RowIndex;

int tableRow = this.Binder.RowIndexToPosition(gridRow);

CurrencyManager cm = (CurrencyManager)this.BindingContext[this.DataSource, this.DataMember];

this.BeginUpdate();

for(int i = sourceRow; i != targetRow; i += dir)

{

if(tableRow + dir < cm.Count && tableRow < cm.Count && tableRow + dir > -1 && tableRow > -1)

{

DataRowView drv1 = (DataRowView) cm.List[tableRow+ dir];

int val1 = (int) drv1.Row["sortKey"];

DataRowView drv2 = (DataRowView) cm.List[tableRow];

int val2 = (int) drv2.Row["sortKey"];

drv1.Row["sortKey"] = val2;

drv2.Row["sortKey"] = val1;

}

tableRow += dir;

}

this.CurrentCell.MoveTo(targetRow,1);

this.EndUpdate();

this.Refresh();

}

inRowMove = false;

}

base.OnMouseUp(e);

}

VB

Public Class DragRowHeaderGrid : Inherits GridDataBoundGrid

Public Sub New()

Me.AllowDrop= False

Me.AllowSelection = Me.AllowSelection And Not GridSelectionFlags.Row

Me.SortBehavior = GridSortBehavior.SingleClick

End Sub

Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)

If inRowMove Then

If targetRow > 0 AndAlso targetCol = 0 AndAlso sourceRow > 0 Then

Dim dir As Integer

If sourceRow > targetRow Then

dir = -1

Else

dir = 1

End If

Dim gridRow As Integer = Me.CurrentCell.RowIndex

Dim tableRow As Integer = Me.Binder.RowIndexToPosition(gridRow)

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

Me.BeginUpdate()

Dim i As Integer = sourceRow

Do While i <> targetRow

If tableRow + dir < cm.Count AndAlso tableRow < cm.Count AndAlso tableRow + dir > -1 AndAlso tableRow > -1 Then

Dim drv1 As DataRowView = CType(cm.List(tableRow+ dir), DataRowView)

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

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

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

drv1.Row("sortKey") = val2

drv2.Row("sortKey") = val1

End If

tableRow += dir

i += dir

Loop

Me.CurrentCell.MoveTo(targetRow,1)

Me.EndUpdate()

Me.Refresh()

End If

inRowMove = False

End If

MyBase.OnMouseUp(e)

End Sub

Here is the link with both CS and VB samples: http://websamples.syncfusion.com/samples/KB/Grid.Windows/GDBG_Drag/main.htm

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
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.

Up arrow icon

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

Live Chat Icon For mobile