繁体   English   中英

C#DataGridView CellDoubleClick事件未触发

[英]C# DataGridView CellDoubleClick Event not firing

我有一个DataFormView设置为只读的WinForm。 当用户双击行(单元格)时,我想显示一个表格,其中显示所选行的完整详细信息。

但是CellDoubleClick事件没有触发。 在网络上的某个地方,我读到了当DataGridView设置为read-only时该事件不起作用,但是即使在MS Visual Studio中插入具有默认设置的新DataGridView也无济于事。

为了进行测试,我使用了MSDN的示例代码,但是MessageBox没有打开。

这是我的完整代码:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Projektmanager
{
    public partial class Projektmanager : Form
    {
        // Declare MySQL Connection
        private MySqlConnection connection;

        public Projektmanager()
        {
            InitializeComponent();

            if (this.OpenConn() == true)
            {
                string sql = "select Name as 'Auftragsnummer', Projektnummer, Gruppe as 'G-Nr.', (select name from Groups where NR=stdjobs.Gruppe limit 1) as 'Gruppe', Händler, Kunde, Land from stdjobs where NR like 'A%' order by NR desc;";
                MySqlDataAdapter MSDA = new MySqlDataAdapter(sql, connection);
                DataSet DS1 = new DataSet();
                MSDA.Fill(DS1);
                dataGridView1.DataSource = DS1.Tables[0];

                this.CloseConn();
            }
        }

        // open MySQL Connection Function
        private bool OpenConn()
        {
            // Some Stuff
        }

        //Close MySQL connection Function
        private bool CloseConn()
        {
           // More Stuff
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void refresh_DGV4()
        {
            // Much more Stuff
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            refresh_DGV4();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            refresh_DGV4();
        }

        private void DataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
        {
            System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex);
            messageBoxCS.AppendLine();
            MessageBox.Show(messageBoxCS.ToString(), "CellDoubleClick Event");
        }
    }
}

谁能告诉我为什么无法识别该活动?

从OP评论:

正如用户Ivan Stoev指出的那样,在MSDN文章中 ,重点是:

若要运行示例代码,请将其粘贴到包含名为[DataGridView1]的类型为[DataGridView]的实例的项目中。 然后,确保事件处理程序与[CellDoubleClick]事件相关联。

如用户OhBeWise所说 ,这可能类似于:

dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;

要么

dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(DataGridVi‌​ew1_CellDoubleClick)‌​;

要验证是否建立了上述关联,请遵循用户Aaron给出的建议,并添加格式:

[L]查找您的[[name] .Designer.cs “文件。 这就是InitializeComponent()调用所调用的。 在这里,您将看到[DataGridView]逻辑。 那将是该事件的附件。 如果不存在,请添加它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM