c#datagridview使用FullRowSelect双击行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13706150/
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
c# datagridview doubleclick on row with FullRowSelect
提问by Metalhead89
I have a datagridview in my C# application and the user should only be able to click on full rows. So I set the SelectionMode to FullRowSelect.
我的 C# 应用程序中有一个 datagridview,用户应该只能单击整行。所以我将 SelectionMode 设置为 FullRowSelect。
But now I want to have an Event which is fired when the user double clicks on a row. I want to have the row number in a MessageBox.
但是现在我想要一个当用户双击一行时触发的事件。我想在 MessageBox 中有行号。
I tried the following:
我尝试了以下方法:
this.roomDataGridView.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.roomDataGridView_CellCont? ?entDoubleClick);
private void roomDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
Unforunately nothing happens. What am I doing wrong?
不幸的是什么也没有发生。我究竟做错了什么?
采纳答案by Pseudonym
Don't manually edit the .designer files in visual studio that usually leads to headaches. Instead either specify it in the properties section of your DataGridRow which should be contained within a DataGrid element. Or if you just want VS to do it for you find the double click event within the properties page->events (little lightning bolt icon) and double click the text area where you would enter a function name for that event.
不要在 Visual Studio 中手动编辑 .designer 文件,这通常会导致头痛。相反,要么在 DataGridRow 的属性部分中指定它,该部分应包含在 DataGrid 元素中。或者,如果您只是希望 VS 为您执行此操作,请在属性页面 -> 事件(小闪电图标)中找到双击事件,然后双击文本区域,在其中输入该事件的函数名称。
This link should help
这个链接应该有帮助
http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx
回答by CR41G14
This will work, make sure your control Event is assigned to this code, it has probably been lost, I also noticed that Double click will only work if the cell is not empty. Try double clicking on a cell with content, don't mess with the designer
这将起作用,确保您的控件事件分配给此代码,它可能已丢失,我还注意到双击仅在单元格不为空时才有效。尝试双击包含内容的单元格,不要惹恼设计师
private void dgvReport_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//do something
}
回答by cihata87
In CellContentDoubleClick event fires only when double clicking on cell's content. I used this and works:
在 CellContentDoubleClick 事件中,仅当双击单元格的内容时才会触发。我使用了这个并且有效:
private void dgvUserList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
回答by Ashraf Abusada
You get the index number of the row in the datagridview using northwind database employees tables as an example:
以northwind数据库员工表为例,获取datagridview中行的索引号:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'nORTHWNDDataSet.Employees' table. You can move, or remove it, as needed.
this.employeesTableAdapter.Fill(this.nORTHWNDDataSet.Employees);
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
var dataIndexNo = dataGridView1.Rows[e.RowIndex].Index.ToString();
string cellValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
MessageBox.Show("The row index = " + dataIndexNo.ToString() + " and the row data in second column is: "
+ cellValue.ToString());
}
}
}
the result will show you index number of record and the contents of the second table column in datagridview:
结果将显示记录的索引号和 datagridview 中第二个表列的内容:
回答by Mahmmoud Kassem
you can do this by : CellDoubleClickEvent
this is code.
您可以通过以下方式执行此操作:CellDoubleClick事件这是代码。
private void datagridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
回答by Christlin Panneer
For your purpose, there is a default event when the row header is double-clicked. Check the following code,
出于您的目的,双击行标题时有一个默认事件。检查以下代码,
private void dgvCustom_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Your code goes here
}
回答by Said El Bakkali
I think you are looking for this: RowHeaderMouseDoubleClick event
我想你正在寻找这个:RowHeaderMouseDoubleClick 事件
private void DgwModificar_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
...
}
to get the row index:
获取行索引:
int indice = e.RowIndex
int indice = e.RowIndex


