Reputation: 417
How do you select a datagridview row on a right-click?
Upvotes: 9
Views: 42594
Reputation: 37
If e.Button = MouseButtons.Right Then
DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex)
End If
code works in VS2019 too
Upvotes: 1
Reputation: 2999
from @Alan Christensen
code convert to VB.NET
Private Sub dgvCustomers_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvCustomers.CellMouseDown
If e.Button = MouseButtons.Right Then
dgvCustomers.CurrentCell = dgvCustomers(e.ColumnIndex, e.RowIndex)
End If
End Sub
I am tested on VS 2017 it working for me!!!
Upvotes: 1
Reputation: 31723
You have to do two things:
Clear all rows and Select the current. I loop through all rows and use the Bool Expression i = e.RowIndex
for this
If you have done Step 1 you still have a big pitfall:
DataGridView1.CurrentRow does not return your previously selected row (which is quite dangerous). Since CurrentRow is Readonly you have to do
Me.CurrentCell = Me.Item(e.ColumnIndex, e.RowIndex)
Protected Overrides Sub OnCellMouseDown(
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
MyBase.OnCellMouseDown(e)
Select Case e.Button
Case Windows.Forms.MouseButtons.Right
If Me.Rows(e.RowIndex).Selected = False Then
For i As Integer = 0 To Me.RowCount - 1
SetSelectedRowCore(i, i = e.RowIndex)
Next
End If
Me.CurrentCell = Me.Item(e.ColumnIndex, e.RowIndex)
End Select
End Sub
Upvotes: 0
Reputation: 847
Make it behave similarly to the left mouse button? e.g.
private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex];
}
}
Upvotes: 24
Reputation: 19353
Subclass the DataGridView
and create a MouseDown
event for the grid,
private void SubClassedGridView_MouseDown(object sender, MouseEventArgs e)
{
// Sets is so the right-mousedown will select a cell
DataGridView.HitTestInfo hti = this.HitTest(e.X, e.Y);
// Clear all the previously selected rows
this.ClearSelection();
// Set as selected
this.Rows[hti.RowIndex].Selected = true;
}
Upvotes: 2
Reputation:
// Clear all the previously selected rows
foreach (DataGridViewRow row in yourDataGridView.Rows)
{
row.Selected = false;
}
// Get the selected Row
DataGridView.HitTestInfo info = yourDataGridView.HitTest( e.X, e.Y );
// Set as selected
yourDataGridView.Rows[info.RowIndex].Selected = true;
Upvotes: 15
Reputation: 9469
You can use JvR's code in the MouseDown event of your DataGridView.
Upvotes: 0
Reputation: 75073
the cool thing is add a menu on that right click, for example with option like "View client information", "verify last invoices", "Add a log entry to this client", etc.
you just need to add a ContextMenuStrip object, add your menu entries, and in the DataGridView properties just select the ContextMenuStrip of it.
This would create a new menu in the row the user right clicked with all the options, then all you need to do is make your magic :)
remember that you need JvR code to get what row was the user in, then grab the cell that contains the Client ID for example and pass that info.
hope it helps improving your application
http://img135.imageshack.us/img135/5246/picture1ku5.png
http://img72.imageshack.us/img72/6038/picture2lb8.png
Upvotes: 5