Drag and Drop in Bound mode

Thread is closed for posting
8 posts, 0 answers
  1. B6B0BF88-AE8B-424C-B71F-0F09B7330C0E
    B6B0BF88-AE8B-424C-B71F-0F09B7330C0E avatar
    1336 posts
    Member since:
    May 2010

    Posted 19 Nov 2010 Link to this post

    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();
                
                     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#
    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
  2. 700E3D64-B0C2-4939-9D66-1A7D605F31FC
    700E3D64-B0C2-4939-9D66-1A7D605F31FC avatar
    62 posts
    Member since:
    Jan 2004

    Posted 31 Aug 2012 Link to this post

    Any chance of redoing this example for VS 2010 and use the Trial Library, as it does not work if you have te trial installed
  3. F11ADD2A-840D-4563-BD9F-564CF2DD488A
    F11ADD2A-840D-4563-BD9F-564CF2DD488A avatar
    2911 posts
    Member since:
    Apr 2022

    Posted 03 Sep 2012 Link to this post

    Hello Denis,

    It does not matter if you are using a trial or dev version. Simply open the solution with Visual Studio 2010 and follow the upgrade wizard to convert it to VS2010 sln. Then change the project references with the one that you have installed and you are good to go.

    Let us know if you have any difficulties achieving this.
     
    All the best,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  4. F11ADD2A-840D-4563-BD9F-564CF2DD488A
    F11ADD2A-840D-4563-BD9F-564CF2DD488A avatar
    2911 posts
    Member since:
    Apr 2022

    Posted 19 Sep 2012 Link to this post

    Today I have updated the example a bit. The changes introduced are that the Handled flag in the PreviewDragDrop event handler should be set only if the drag instance is GridDataRowElement:
    private void dragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)
    {
        var rowElement = e.DragInstance as GridDataRowElement;
        if (rowElement == null)
        {
            return;
        }
      
        e.Handled = true;
    ........................

    Additionally, the following loop in the MoveRows method:
    for (int j = 0; j < row.Cells.Count; j++)
    {
        newRow[j] = row.Cells[j].Value;
    }

    should be replaced with this one, so if one changes a column position the values as assigned correctly:
    foreach (GridViewCellInfo cell in row.Cells)
    {
        newRow[cell.ColumnInfo.Name] = cell.Value;
    }


    Greetings,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  5. 1DCB61D8-5042-4E55-8C44-787410FFE24C
    1DCB61D8-5042-4E55-8C44-787410FFE24C avatar
    4 posts
    Member since:
    May 2013

    Posted 28 May 2013 Link to this post

    I am using linq and entity framewok
    But your code does not work

    public partial class Personnel
        {
            public Personnel()
            {
            }
         
            public int PersonnelID { get; set; }
            public string FirstNameFa { get; set; }
            public string LastNameFa { get; set; }
            public string FirstNameEn { get; set; }
            public string LastNameEn { get; set; }
    }


    RAYANContext db = new RAYANContext();
    radGridView1.DataSource =db.Personnels.Select(r=>r).ToList() ;
    radGridView2.DataSource = db.Personnels.Select(r => r).Where(p=>p.PersonnelID==0).ToList();
  6. F11ADD2A-840D-4563-BD9F-564CF2DD488A
    F11ADD2A-840D-4563-BD9F-564CF2DD488A avatar
    2911 posts
    Member since:
    Apr 2022

    Posted 03 Jun 2013 Link to this post

    Hello Ali,

    Can you please have a look at the following blog post, where you can find a more recent version of an example demonstrating drag and drop between RadGridViews: http://blogs.telerik.com/winformsteam/posts/13-05-15/extending-radgridview-to-enable-row-drag-and-drop-functionality.
     
    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  7. 79ACB56A-601C-4BC1-AE5A-7E51A9C33D37
    79ACB56A-601C-4BC1-AE5A-7E51A9C33D37 avatar
    4 posts
    Member since:
    May 2011

    Posted 22 Sep 2015 Link to this post

    am using visual studio 2010 windows application,

    in that i want to add a drag and drop RridviewRow from one grid to another grid, GridviewRow have multiple column , tried some sample from forum but i din't , 

    am loading Source Grid Data from DataTable.

     

    help me ...

  8. F11ADD2A-840D-4563-BD9F-564CF2DD488A
    F11ADD2A-840D-4563-BD9F-564CF2DD488A avatar
    2911 posts
    Member since:
    Apr 2022

    Posted 22 Sep 2015 Link to this post

    Hello Baskar,

    The solution provided by Emanuel applies to Telerik UI for WinForms Q3 2010. You can test it how it works with your version. Alternatively, you can check the online documentation on the matter: http://www.telerik.com/help/winforms/gridview-rows-drag-and-drop.html. Should you still have issues, feel free to contact our support team by opening a support ticket.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.