locked
MouseDown Event procedure on Drag Drop routines RRS feed

  • Question

  • Hello Everyone:

    I'm trying to drag drop employee records from one DataGridView to another DataGridView:

    Following is the MouseDown event routine from the Unassigned Employee Grid View

    private void Unassigned_dataVW_MouseDown(object sender, MouseEventArgs e)        {            // test to make sure the user has selected a value, before submitting value to a variable.            if (this.Unassigned_dataVW.SelectedRows != null)            {                // Grab selected value and "cast" it to an integer                int rowIndx = Convert.ToInt32(this.Unassigned_dataVW.SelectedCells[0].RowIndex);                EmployeeID = Convert.ToInt32(this.Unassigned_dataVW[0, rowIndx].Value);                Console.Write(".. Leaving mouse down: " + EmployeeID);                // Feed EmployeeID to DragDropEffects - so that it can be carried to receiving station                DragDropEffects dde1 = DoDragDrop(EmployeeID, DragDropEffects.All);             }            else            {                return;            }        }

    The routine essentially works, but the routine is picking up the EmployeeID of the record already selected BEFORE the employee did a "mouse down" on the current record selector.

    I've tried putting this routine in the following event procedures:

    1. SelectionChanged
    2. RowHeaderMouseClick
    3. MouseClicke

    Those are just some of the events I try to use. None of them work.

    I've tried resetting the EmployeeID variable to 0 BEFORE this statement:

    EmployeeID = Convert.ToInt32(this.Unassigned_dataVW[0, rowIndx].Value);  

    But resetting to 0, before grabbing new EmployeeID still outputs id of previous record.

    Any suggestions on how to grab the correct id BEFORE dragging and dropping are welcome. All too often, it is possible for a user to select, drag the wrong unassigned employee and drop them in a new station DataGridView.

    Thanks in advance - Pavilion


    • Moved by CoolDadTx Monday, January 27, 2014 3:26 PM Winforms related
    Monday, January 27, 2014 2:45 PM

Answers

  • Hi Pavilion,

    I'm not so clear about your issue right now,  but I think this link is helpful to understand those events better:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragenter.aspx

    Check the following:

    “The following describes how and when events related to drag-and-drop operations are raised.

    The DoDragDrop method determines the control under the current cursor location. It then checks to see if the control is a valid drop target.

    If the control is a valid drop target, the GiveFeedback event is raised with the drag-and-drop effect specified. For a list of drag-and-drop effects, see the DragDropEffects enumeration.

    Changes in the mouse cursor position, keyboard state, and mouse button state are tracked.

    • If the user moves out of a window, the DragLeave event is raised.

    • If the mouse enters another control, the DragEnter for that control is raised.

    • If the mouse moves but stays within the same control, the DragOver event is raised.

    If there is a change in the keyboard or mouse button state, the QueryContinueDrag event is raised and determines whether to continue the drag, to drop the data, or to cancel the operation based on the value of the Action property of the event's QueryContinueDragEventArgs.”

    “The drag action starts if the mouse has moved more than SystemInformation.DragSize from the mouse location during the MouseDown event. The IndexFromPoint method is used to determine the index of the item to drag during the MouseDown event.”

    Then I checked that SelectedCells from http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells(v=vs.110).aspx

    I suppose your problem may due to this:

    "This property contains a read-only snapshot of the selection at the time it is referenced. If you hold onto a copy of this collection, it may differ from the actual, subsequent DataGridView state in which the user may have changed the selection. Therefore, you should not operate on a copy of the collection. "

    What about try get data without using select? Just pass that clicked cell value? Or use some cellcontent click or cellclick event?

    Regards,



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Barry Wang Monday, February 10, 2014 5:35 AM
    Wednesday, January 29, 2014 9:40 AM