RadControls version
|
Q3 2010+ |
.NET version |
3.5 |
Visual Studio version |
2008, 2010 |
programming language |
CS, VB |
Drag & Drop Between grids in unbound mode
This project is intended to provide an easy way to implement drag and drop operations between grids in bound mode. This example covers both DataSet's and Business objects, for other types please address the following method:
C#:
private
void
MoveRows(RadGridView targetGrid, RadGridView dragGrid, IList<GridViewRowInfo> dragRows,
int
index)
{
dragGrid.BeginUpdate();
targetGrid.BeginUpdate();
for
(
int
i = dragRows.Count - 1; i >= 0; i--)
{
GridViewRowInfo row = dragRows[i];
if
(row
is
GridViewSummaryRowInfo)
{
continue
;
}
if
(
typeof
(DataSet).IsAssignableFrom(targetGrid.DataSource.GetType()))
{
// customize data set tables here if required
var sourceTable = ((DataSet)dragGrid.DataSource).Tables[0];
var targetTable = ((DataSet)targetGrid.DataSource).Tables[0];
var newRow = targetTable.NewRow();
foreach
(GridViewCellInfo cell
in
row.Cells)
{
newRow[cell.ColumnInfo.Name] = cell.Value;
}
sourceTable.Rows.Remove(((DataRowView)row.DataBoundItem).Row);
targetTable.Rows.InsertAt(newRow, index);
}
else
if
(
typeof
(IList).IsAssignableFrom(targetGrid.DataSource.GetType()))
{
var targetCollection = (IList)targetGrid.DataSource;
var sourceCollection = (IList)dragGrid.DataSource;
// handle custom removing of data bound item here if necessary
sourceCollection.Remove(row.DataBoundItem);
// format insert data here
targetCollection.Insert(index, row.DataBoundItem);
}
else
{
// handle custom data operations here
}
}
dragGrid.EndUpdate(
true
);
targetGrid.EndUpdate(
true
);
}
or VB:
Private
Sub
MoveRows(
ByVal
targetGrid
As
RadGridView,
ByVal
dragGrid
As
RadGridView, _
ByVal
dragRows
As
IList(Of GridViewRowInfo),
ByVal
index
As
Integer
)
dragGrid.BeginUpdate()
targetGrid.BeginUpdate()
For
i
As
Integer
= dragRows.Count - 1
To
0
Step
-1
Dim
row
As
GridViewRowInfo = dragRows(i)
If
TypeOf
row
Is
GridViewSummaryRowInfo
Then
Continue
For
End
If
If
GetType
(DataSet).IsAssignableFrom(targetGrid.DataSource.[
GetType
]())
Then
' customize data set tables here if required
Dim
sourceTable =
DirectCast
(dragGrid.DataSource, DataSet).Tables(0)
Dim
targetTable =
DirectCast
(targetGrid.DataSource, DataSet).Tables(0)
Dim
newRow = targetTable.NewRow()
For Each cell As GridViewCellInfo In row.Cells
newRow(cell.ColumnInfo.Name) = cell.Value
Next
sourceTable.Rows.Remove(
DirectCast
(row.DataBoundItem, DataRowView).Row)
targetTable.Rows.InsertAt(newRow, index)
ElseIf
GetType
(IList).IsAssignableFrom(targetGrid.DataSource.[
GetType
]())
Then
Dim
targetCollection =
DirectCast
(targetGrid.DataSource, IList)
Dim
sourceCollection =
DirectCast
(dragGrid.DataSource, IList)
' handle custom removing of data bound item here if necessary
sourceCollection.Remove(row.DataBoundItem)
' format insert data here
targetCollection.Insert(index, row.DataBoundItem)
' handle custom data operations here
Else
End
If
Next
dragGrid.EndUpdate(
True
)
targetGrid.EndUpdate(
True
)
End
Sub
Also if you are using dragging multiple rows you could consider using a custom drag hint, this can be achieved in this method:
C#
VB:
Hope you find this project helpful, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP