Ticket T638111
Visible to All Users

Drag & Drop between two grids

created 7 years ago (modified 7 years ago)

My situation is that I have company records with each company having multiple locations, with each location having multiple items allocated to them.

I have 2 grids using the same bindingsource. One is for items that have already been assigned to a companies' location, and the other contains items that have been assigned to the company but not yet to a specific location.

The first grid has its ActiveFilterString set to "LocationID ='[a valid guid]'" and the second grid's ActiveFilterString is "LocationID IS NULL and CompanyID='[a valid guid]'".

I have downloaded the E694 example (DragDropTwoGrids) but I am wanting to use the new drag-drop behaviours of the BehaviourManager slightly differently and would appreciate your advice on how to change the dragged row's LocationID to either NULL (if dragging to the unallocated grid) or to a predetermined LocationID value (if dragging to the allocated grid) (which comes from a focused TreeList node) - without having to revert to the old way of dragging and dropping.

Also, can this be done without the grids' InitNewRow() events firing?

Many thanks,
Richard

Answers approved by DevExpress Support

created 7 years ago

Hello Richard,

To complete this task, handle a behavior's DragDrop  event. You can do it at design time by clicking the Edit Behaviors command in BehaviorManager's smart tag. In the DragDrop  event, obtain a dragged row's row handle using the e.Data  parameter and set the LocationID property to the necessary value. Also, set the e.Handled parameter to true to disable further event processing.

Attached is a small sample that illustrates the basic idea. Please review it and let me know if it helps.

See also:
Drag And Drop Behavior

    Comments (1)
    RM RM
    Richard McNaught 1 7 years ago

      Hi Sasha,

      Thanks for the response. I've made changes to my code (please see below) similar to the sample application you sent but I'm still not able to drag from one grid to the other.

      I notice that the example you sent uses integers for the primary key, whereas I'm using guids - I raised a ticket recently (T634142) regarding treeviews where it didn't seem to like using guids - perhaps this is the same problem? I've tried setting the LocationID to nulland to DBNull.Value, when 'unallocating' an item but neither work. Also, when using the GetRowCellValue() method after setting the value, I see that the value is not being changed - I've tried the actual guid and the guid.ToString()

      I also notice that neither grid in the example uses a filter - In my case one grid filters for non-allocated items and the other filters for allocated items - this could be confusing the DragDrop event because both grids use the same binding source, but the table adapter isn't updated until later when the user clicks a SAVE button.

      The act of dropping a row onto the other grid should mean that the row in one grid does not now match its filter, and the row in the other grid should now be a match with the filter - however, dropping the row does nothing (please see attached video).

      The code I am now using is:

      C#
      private void dragDropEvents1_DragDrop_1(object sender, DragDropEventArgs e) { var handles = (int[])e.Data; GridView tar = e.Target as GridView; if (tar.Name == "gridView1") { Guid? lhid = treeList1.FocusedNode.GetValue("LocationHierarchyID") as Guid?; (e.Target as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", lhid); //(e.Target as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", lhid.ToString()); //(e.Source as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", null); (e.Source as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", DBNull.Value); } else { //(e.Target as GridView).SetRowCellValue(handles[0],"LocationHierarchyID", null); //(e.Source as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", null); (e.Target as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", DBNull.Value); (e.Source as GridView).SetRowCellValue(handles[0], "LocationHierarchyID", DBNull.Value); } e.Handled = true; }

      One grid is a copy of the other grid, but they are given different filters in the page load() event, and the column names differ as duplicates are not permitted. They both allow editing (which is denied in the example you sent) because there are many hidden columns which are displayed in an EditForm. There are also 11 repository editors (mostly gridlookupedits) in both.

      Other Answers

      created 7 years ago (modified 7 years ago)

      Hello again Sasha,

      Because both grids are bound to the same binding source, the answer is to only change the LocationID of the source and the target automatically updates:

      C#
      private void dragDropEvents1_DragDrop_1(object sender, DragDropEventArgs e) { GridView source = e.Source as GridView; DataRowView r = (DataRowView)source.GetFocusedRow(); if (source.Name == "gridView2") { Guid? lhid = treeList1.FocusedNode.GetValue("LocationHierarchyID") as Guid?; r["LocationHierarchyID"] = lhid; } else { r["LocationHierarchyID"] = DBNull.Value; } e.Action = DragDropActions.None; e.Handled = true; }

      Thanks for your help.
      Richard

        Comments (3)
        Sasha (DevExpress Support) 7 years ago

          Hello Richard,

          I am happy to hear that you found an appropriate solution. Thank you for informing us.
          Should you have further questions, let me know.

            Hi
            I'm trying to move a row to an empty grid, in Behavior_DragOver (object sender, DragOverEventArgs e) I am getting var args = DragOverGridEventArgs.GetDragOverGridEventArgs (e); value InsertType = None. As a result, the layout of the line is not drawn, and the cursor informs about the impossibility of moving. Should I enable additional options?

            Robert Troska

            DevExpress Support Team 5 years ago

              Hello,

              I've created a separate ticket on your behalf (T835296: How to drag a row to an empty grid). It has been placed in our processing queue and will be answered shortly.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.