Drag-Drop is one of the most common practices used in any Grid. It is basically used to rearrange the position of Rows and Columns based on the needs of the user. On one hand, the Drag-Drop of Columns is pretty easy when it comes into play within the same grid and it may be achieved without any snag. Now on the other hand, the Drag-Drop of Rows may bring a twist if used within a bound grid. Row_Dragging If the grid is unbound, the Drag-Drop of Rows works perfectly well. In the case where the grid is bound to a DataTable, an ArgumentException is thrown while Dragging and Dropping rows. This occurs because of the DataSource. When a row is dragged, the grid first removes the row from its original position and then re-inserts it into a new position. In bound mode, this requires calling methods on data source object.

Seems a bit of a hard nut to crack!

However, 'Nothing is Impossible' in the programming world. Here are some simple steps to achieve Drag-Drop of Rows in a bound grid. 1. Make sure to assign AllowDragging property of FlexGrid to 'Rows'.


c1FlexGrid1.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.Rows;  

2. In BeforeDragRow event of FlexGrid, first take out the data stored in the row which is dragged and the row to which it is dropped.


System.Data.DataRow drDragged = dt.Rows[e.Row - 1];  
System.Data.DataRow drNewPosition = dt.Rows[e.Position - 1];  
object[] arr1 = drDragged.ItemArray;  
object[] arr2 = drNewPosition.ItemArray;  
DataRow dr1 = dt.NewRow();  
dr1.ItemArray = arr1; //Temporary Row to store data of row being dragged  
DataRow dr2 = dt.NewRow();  
dr2.ItemArray = arr2; //Temporary Row to store data of row at which it is dropped  

3. Remove the Rows from DataSource and insert them at their swapped location.


dt.Rows.Remove(drDragged);  
dt.Rows.Remove(drNewPosition);  
dt.Rows.InsertAt(dr1, e.Position - 1); //Inserting Rows at Swapped Postions  
dt.Rows.InsertAt(dr2, e.Row - 1);  

4. Re-assign updated DataSource to the grid.


c1FlexGrid1.DataSource = dt;  

5. Suppress the default dragging behavior.


e.Cancel = true; // Suppress default dragging behaviour  

The best thing with this method is that it can be used with any type of grid. Download C# Sample Download VB Sample