C#DataGridView CellDoubleClick事件未触发
我有一个DataGridView设置为只读的WinForm。当用户双击一行(单元格)时,我希望显示一个表单,其中显示所选行的完整详细信息 但是CellDoubleClick事件没有触发。我在网上的某个地方读到,当DataGridView设置为只读时,该事件不起作用,但即使在MS Visual Studio中插入带有默认设置的新DataGridView也没有帮助 对于测试,我使用了来自的示例代码,但MessageBox没有打开 以下是我的完整代码: 表格1.cs:C#DataGridView CellDoubleClick事件未触发,c#,winforms,C#,Winforms,我有一个DataGridView设置为只读的WinForm。当用户双击一行(单元格)时,我希望显示一个表单,其中显示所选行的完整详细信息 但是CellDoubleClick事件没有触发。我在网上的某个地方读到,当DataGridView设置为只读时,该事件不起作用,但即使在MS Visual Studio中插入带有默认设置的新DataGridView也没有帮助 对于测试,我使用了来自的示例代码,但MessageBox没有打开 以下是我的完整代码: 表格1.cs: using System; us
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所指出的,emphasis补充道: 要运行示例代码,请将其粘贴到包含名为DataGridView1的[DataGridView]类型实例的项目中然后确保事件处理程序与[CellDoubleClick]事件关联。 如用户Ohse所述,这可能看起来像:
dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;
或
要验证是否建立了上述关联,请遵循用户Aaron给出的建议,添加格式:
[五十] 在“[name].Designer.cs”文件中查找。这就是使用InitializeComponent()
调用调用的内容。在这里,您将看到[DataGridView]逻辑。这就是它本应附属于该活动的地方。如果它不在那里,添加它
您是否已使用
dataGridView1.CellDoubleClick+=dataGridView1\u CellDoubleClick验证句柄是否已附加到事件
或dataGridView1.CellDoubleClick+=new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1\u CellDoubleClick)代码>?“若要运行示例代码,请将其粘贴到包含名为DataGridView1的DataGridView类型实例的项目中。然后确保事件处理程序与CellDoubleClick事件关联。”若要根据Ohbouse的注释展开,请查看“[name].Designer.cs”文件。这就是InitializeComponent()调用的内容。在这里,您将看到datagridview逻辑。这就是它本应附属于该活动的地方。如果没有,请加上。谢谢。明天早上我就试试。好的。我刚刚将它添加到*.Design.cs文件中,现在它可以工作了。
dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(DataGridView1_CellDoubleClick);