c #datagridview使用FullRowSelect双击行

时间:2012-12-04 15:21:52

标签: c# events datagridview double-click

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

但是现在我想要一个在用户双击一行时触发的事件。我想在MessageBox中输入行号。

我尝试了以下内容:

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

不幸的是没有任何反应。我做错了什么?

7 个答案:

答案 0 :(得分:18)

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

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

答案 1 :(得分:6)

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

此链接应该有帮助

http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx

答案 2 :(得分:4)

这将有效,请确保您的控件事件已分配给此代码,它可能已丢失,我还注意到双击仅在单元格不为空时才有效。尝试双击包含内容的单元格,不要惹恼设计师

 private void dgvReport_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {

   //do something


 }

答案 3 :(得分:2)

您可以使用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());
        }
    }
}

结果将显示datagridview中记录的索引号和第二个表列的内容:

enter image description here

答案 4 :(得分:0)

您可以通过以下方式执行此操作:CellDoubleClick事件 这是代码。

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

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

我想您正在寻找的是:RowHeaderMouseDoubleClick事件

private void DgwModificar_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
...
}

获取行索引:

int indice = e.RowIndex