Datagridview Determine if right click is on selected cells

  • Thread starter Thread starter Bill Schanks
  • Start date Start date
B

Bill Schanks

VB 2005

How do I tell if a right click on a datagridview is in the selected
cells? I want to selected the cell if it's not, and if it is in the
selected cell range do nothing.

I have this code to select the right clicked cell, but how do I check
if it's in the selected cell range?

Private Sub DataGridView_CellMouseDown(ByVal sender As System.Object,
_
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles DataGridView.CellMouseDown

If e.Button = Windows.Forms.MouseButtons.Right Then
Me.DataGridView.CurrentCell =
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
End If

End Sub
 
you can try something like this:

Dim SelectedCells As DataGridViewSelectedCellCollection =
datagridview1.SelectedCells

For Each cel As DatagridviewCell in SelectedCells
Debug.Print cel.ColumnIndex.ToString & " " cel.RowIndex.ToString
Next

Rich
 
you can try something like this:

Dim SelectedCells As DataGridViewSelectedCellCollection =
datagridview1.SelectedCells

For Each cel As DatagridviewCell in SelectedCells
   Debug.Print cel.ColumnIndex.ToString & " " cel.RowIndex.ToString
Next

Rich

*** Sent via Developersdexhttp://www.developersdex.com***

Thanks, got this work with reasonable performance:

If e.Button = Windows.Forms.MouseButtons.Right Then
Dim SelectedCells As DataGridViewSelectedCellCollection =
Me.DataGridView.SelectedCells
Dim bInselection As Boolean = False

'Check to see if click in selected range.. if it is do nothing
For Each cel As DataGridViewCell In SelectedCells
If e.ColumnIndex >= cel.ColumnIndex AndAlso _
e.ColumnIndex <= cel.ColumnIndex AndAlso _
e.RowIndex >= cel.RowIndex AndAlso _
e.RowIndex <= cel.RowIndex Then

bInselection = True
Exit For
End If
Next

'Select the cell, if it isn't in the current selected range
If Not bInselection Then _
Me.DataGridView.CurrentCell =
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)

End If
 
Thanks, got this work with reasonable performance:

If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim SelectedCells As DataGridViewSelectedCellCollection =
Me.DataGridView.SelectedCells
        Dim bInselection As Boolean = False

        'Check to see if click in selected range.. if it is do nothing
        For Each cel As DataGridViewCell In SelectedCells
                If e.ColumnIndex >= cel.ColumnIndex AndAlso _
                 e.ColumnIndex <= cel.ColumnIndex AndAlso _
                 e.RowIndex >= cel.RowIndex AndAlso _
                 e.RowIndex <= cel.RowIndex Then

                        bInselection = True
                        Exit For
                End If
        Next

        'Select the cell, if it isn't in the current selected range
        If Not bInselection Then _
           Me.DataGridView.CurrentCell =
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)

End If

For those interested I found a much cleaner approach:

Private Sub DataGridView_CellMouseDown(ByVal sender As System.Object,
_
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles DataGridView.CellMouseDown

If e.Button = Windows.Forms.MouseButtons.Right AndAlso _
Not
Me.DataGridView.SelectedCells.Contains(Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex))
Then _
Me.DataGridView.CurrentCell =
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)

End Sub
 
Back
Top