From: Bill Schanks on
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
From: Rich P on
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 Developersdex http://www.developersdex.com ***
From: Bill Schanks on
On Feb 11, 1:01 pm, Rich P <rpng...(a)aol.com> wrote:
> 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
From: Bill Schanks on
On Feb 11, 3:29 pm, Bill Schanks <wscha...(a)gmail.com> wrote:
> On Feb 11, 1:01 pm, Rich P <rpng...(a)aol.com> wrote:
>
> > 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

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