c#datagridview使用FullRowSelect双击行

c#datagridview使用FullRowSelect双击行,c#,events,datagridview,double-click,C#,Events,Datagridview,Double Click,我的C#应用程序中有一个datagridview,用户应该只能单击整行。因此,我将SelectionMode设置为FullRowSelect 但是现在我想要一个当用户双击一行时触发的事件。我想把行号放在消息框中 我尝试了以下方法: this.roomDataGridView.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.roomDataGridView_CellCont‌

我的C#应用程序中有一个datagridview,用户应该只能单击整行。因此,我将SelectionMode设置为FullRowSelect

但是现在我想要一个当用户双击一行时触发的事件。我想把行号放在消息框中

我尝试了以下方法:

 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());
 }

不幸的是什么也没发生。我做错了什么?

这会起作用,请确保您的控件事件已分配给此代码,它可能已丢失,我还注意到双击仅在单元格不为空时起作用。尝试双击一个包含内容的单元格,不要弄乱设计器

 private void dgvReport_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {

   //do something


 }

不要在visual studio中手动编辑通常会导致头痛的.designer文件。而是在DataGridRow的properties部分指定它,DataGridRow应该包含在DataGrid元素中。或者,如果您只想让VS为您执行此操作,请在属性页->事件(小闪电图标)中找到双击事件,然后双击文本区域,在该区域中输入该事件的函数名

这个链接应该会有所帮助


在CellContentDoubleClick中,只有双击单元格内容时才会触发DoubleClick事件。我使用了这个,并且工作:

    private void dgvUserList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        MessageBox.Show(e.RowIndex.ToString());
    }

以northwind数据库employees表为例,获取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());
        }
    }
}
结果将显示记录的索引编号和datagridview中第二个表列的内容:


您可以通过:
CellDoubleClick
事件 这是代码

private void datagridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        MessageBox.Show(e.RowIndex.ToString());
    }

出于您的目的,双击行标题时会出现默认事件。检查以下代码

 private void dgvCustom_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
 {
      //Your code goes here
 }

我想您正在寻找这个:RowHeaderMouseDoubleClick事件

private void DgwModificar_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
...
}
要获取行索引,请执行以下操作:


int indice=e.RowIndex

如何订阅双击事件?我在设计器中编写了这个.roomDataGridView.CellContentDoubleClick+=new System.Windows.Forms.DataGridViewCellEventHandler(这个.roomDataGridView\u CellContentDoubleClick);我刚刚删除了我的活动,又做了一次,现在它工作了。我真的不知道发生了什么,但它起作用了now@Metalhead89:在
中有一个空格(this.roomDataGridView\u CellContentDoubleClick)确保处理CellContentsDoubleClick或CellDoubleClick,而不是两者,因为它们会相互抵消(假设您正在做相同的事情)。基本上他们都会被炒鱿鱼。你的答案不完整,你没有解释如何添加事件侦听器,等等……我哥哥不明白什么?