Stop selecting in Datagridview

Facing a problem and not sure it can be solve or not. And my english is a bit weak, hope you still able to understand what i am trying to tell.

In my program there are 2 main portion, left part is a treeview, right part is a datagridview. I want to implement drag and drop from datagridview's row(s). When you mouse down(left button) on datagridview, and move the mouse towards down the screen, it will select multiple rows for you, this is correct and I want it remains, but I want it to stop selecting if I drag my mouse towards left into the treeview, I will code the drag and drop operation in the treeview, but the problem is how do I code to stop the datagridview selecting mode ?

Any response is welcome, thanks in advance.

[735 byte] By [WongJunnKit] at [2008-2-13]
# 1
Once you have initiated the dragdrop operation (through calling DoDragDrop), the DataGridView selection doesn't change. Here is some code to demonstrate determining when to initiate the operation. It also demonstrates determining dragging within the control:



Rectangle m_dragBoxFromMouseDown;

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)

{

//Calculate the size of the drag box for internal dragging

Size dragSize = SystemInformation.DragSize;

Point pointTopLeft = new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2));

m_dragBoxFromMouseDown = new Rectangle(pointTopLeft, dragSize);

dataGridView1.MultiSelect = true;

}

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)

{

//Check to see if user is initiating drag

if (e.Button == MouseButtons.Left && !(new Rectangle(dataGridView1.Location, dataGridView1.Size).Contains(e.Location)))

{

// user has dragged outside the control

DoDragDrop(dataGridView1.SelectedCells, DragDropEffects.Copy);

}

else if (e.Button == MouseButtons.Left && !m_dragBoxFromMouseDown.Contains(e.Location))

{

// user has dragged within the control

}

else

{

// user not dragging

}

}



durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

If 5 rows were selected before MouseDown then all but the row under the cursor are unselected. Any idea on how to keep the other rows selected so that multiple rows can be dragged?

Thanks in advance.

vala at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

As far as I know (and I've tested a lot...) the un-selection of these multiple rows cannot be prevented directly. I found that it's possible to restore the selection using the following code:

Code Snippet

' Global list to save selected indexes

Private mSelectedRows As Collections.Generic.List(Of Integer) = New Collections.Generic.List(Of Integer)

Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, _

ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
Dim iIndex As Integer

' Reset information on saved selected indexes
mSelectedRows.Clear()

' Don't save selection of Shift or Ctrl key is used to alter selection
If Not (My.Computer.Keyboard.CtrlKeyDown OrElse My.Computer.Keyboard.ShiftKeyDown) Then
' Get index of row clicked and check if it's selected
iIndex = e.RowIndex
If iIndex >= 0 AndAlso DataGridView1.Rows(iIndex).Selected Then
' Save indexes of all selected rows to list
For Each oRow As DataGridViewRow In DataGridView1.SelectedRows
mSelectedRows.Add(oRow.Index)
Next
End If
End If
End Sub

Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Static bRestoringSelection As Boolean = False

' Flag to prevent recursion of this event
If bRestoringSelection Then Exit Sub

' Check if saved indexes of selected rows exist
If mSelectedRows.Count > 0 Then
Dim oRows As Collections.Generic.List(Of Integer)

' Set recursion flag and suspend visual updates
bRestoringSelection = True
DataGridView1.SuspendLayout()

' Restore selection
For Each iIndex As Integer In oRows
DataGridView1.Rows(iIndex).Selected = True
Next

' Clear information on saved selection indexes
mSelectedRows.Clear()

' Resume layout and reset recursion flag
DataGridView1.ResumeLayout()
bRestoringSelection = False
End If
End Sub

I hope that helps, for me it's working fine.

Ulrich

Ulrich at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...