我有一个WinForm,DataGridView设置为只读。当用户双击一行(单元格)时,我想显示一个表单,其中显示所选行的完整详细信息。
但是CellDoubleClick事件没有触发。在网络的某个地方,我已经读到当DataGridView设置为read-only
时事件不起作用,但即使在MS Visual Studio中使用默认设置插入新的DataGridView也没有帮助。
For Testing我使用了来自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");
}
}
}
有谁能告诉我为什么事件不被认可?
答案 0 :(得分:0)
来自OP评论:
正如用户 Ivan Stoev 所指出的那样,MSDN article重点强调:
要运行示例代码,请将其粘贴到包含名为DataGridView1的[DataGridView]类型实例的项目中。 然后确保事件处理程序与[CellDoubleClick]事件相关联。
如用户 OhBeWise 所述,这可能如下所示:
dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;
或
dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(DataGridView1_CellDoubleClick);
要验证上述关联,请按照用户 Aaron 提供的建议,添加格式:
[L]在你的" [name] .Designer.cs "文件。这是通过
InitializeComponent()
调用调用的内容。在那里,您将看到[DataGridView]逻辑。这是它将附加到事件的地方。如果不存在,请添加它。