.net 右键单击以选择一个 datagridview 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/173295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right-click to select a datagridview row
提问by Jeffrey A. Reyes
How do you select a datagridview row on a right-click?
如何在右键单击时选择 datagridview 行?
回答by Alan Christensen
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];
}
}
回答by Alan Christensen
// 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;
回答by balexandre
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.
您只需要添加一个 ContextMenuStrip 对象,添加您的菜单项,然后在 DataGridView 属性中选择它的 ContextMenuStrip。
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.
请记住,您需要 JvR 代码来获取用户所在的行,然后获取包含客户端 ID 的单元格并传递该信息。
hope it helps improving your application
希望它有助于改进您的应用程序
http://img135.imageshack.us/img135/5246/picture1ku5.png
http://img135.imageshack.us/img135/5246/picture1ku5.png
回答by Brendan
Subclass the DataGridViewand create a MouseDownevent for the grid,
子类化DataGridView并MouseDown为网格创建一个事件,
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;
}
回答by Johann Blais
You can use JvR's code in the MouseDown event of your DataGridView.
您可以在 DataGridView 的 MouseDown 事件中使用 JvR 的代码。
回答by Jürgen Steinblock
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.RowIndexfor thisIf 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 doMe.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
清除所有行并选择当前行。我遍历所有行并
i = e.RowIndex为此使用 Bool 表达式如果您已经完成了第 1 步,您仍然有一个大陷阱:
DataGridView1.CurrentRow 不会返回您之前选择的行(这很危险)。由于 CurrentRow 是只读的,你必须做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
回答by IT Vlogs
from @Alan Christensencode convert to VB.NET
从@Alan Christensen代码转换为 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!!!
我在 VS 2017 上进行了测试,它对我有用!!!

