Skip Navigation LinksHome / Community & Support / Code Library / WinForms > GridView > Drag and Drop in Bound mode

Not answered Drag and Drop in Bound mode

Feed from this thread
  • Posted on Nov 19, 2010 (permalink)

    Requirements

    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();
                for (int j = 0; j < row.Cells.Count; j++)
                {
                    newRow[j] = row.Cells[j].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 j As Integer = 0 To row.Cells.Count - 1
                    newRow(j) = row.Cells(j).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#
    private void dragDropService_PreviewDragHint(object sender, PreviewDragHintEventArgs e)
    {
        var dataRowElement = e.DragInstance as GridDataRowElement;
        if (dataRowElement != null && dataRowElement.ViewTemplate.MasterTemplate.SelectedRows.Count > 1)
        {
            // set custom drag hint for multiple rows here
            //e.DragHint = new Bitmap(@"SomeLocation/SomeFile");
            //e.UseDefaultHint = false;
        }
    }

    VB:
    Private Sub dragDropService_PreviewDragHint(ByVal sender As Object, ByVal e As PreviewDragHintEventArgs) _
        Handles dragDropService.PreviewDragHint
        Dim dataRowElement = TryCast(e.DragInstance, GridDataRowElement)
        ' set custom drag hint for multiple rows here
        'e.DragHint = new Bitmap(@"SomeLocation/SomeFile");
        'e.UseDefaultHint = false;
        If dataRowElement IsNot Nothing AndAlso dataRowElement.ViewTemplate.MasterTemplate.SelectedRows.Count > 1 Then
        End If
    End Sub

    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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / WinForms > GridView > Drag and Drop in Bound mode