C# WinFormDataGrid双击事件
datagrid是否存在双击事件?我试图使用此代码在用户双击某行时打开详细信息表单 我试图通过双击控件添加它,但它提供了dataGrid1\u导航功能。也许您可以使用该事件 例如:C# WinFormDataGrid双击事件,c#,.net,C#,.net,datagrid是否存在双击事件?我试图使用此代码在用户双击某行时打开详细信息表单 我试图通过双击控件添加它,但它提供了dataGrid1\u导航功能。也许您可以使用该事件 例如: private void DataGridView1_CellContentDoubleClick(Object sender, DataGridViewCellEventArgs e) { System.Text.StringBuilder messageBoxCS = new System.Text.St
private void DataGridView1_CellContentDoubleClick(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(), "CellContentDoubleClick Event" );
}
如果这不是您要查找的,您可以在参考中找到其他事件:
听起来您需要一种方法来获取给定控件的所有事件的列表,而不是在设计器中双击控件时查找VS提供的默认事件 有几种方法可以做到这一点: 单向选择网格。 然后单击事件图标将属性窗口变成事件列表,然后双击要对事件进行编码的事件
或者,切换到“代码视图”,在“代码”窗口左上角的对象下拉列表中选择网格,然后从代码窗口右上角的事件列表中,从该控件的所有事件列表中选择所需的事件在设计模式下双击控件时得到的是控件设计者认为最常用的事件,在本例中是导航 但是,此控件有两个双击事件:
public partial class Form1 : Form
{
DataGrid grid = new DataGrid();
public Form1()
{
InitializeComponent();
grid.DoubleClick += new EventHandler(grid_DoubleClick);
grid.MouseDoubleClick += new MouseEventHandler(grid_MouseDoubleClick);
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
}
void grid_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
void grid_DoubleClick(object sender, EventArgs e)
{
}
}
但是,当您双击控件上的任意位置时,这两个事件都会运行,并且它们不会直接向您提供有关所选行的信息。通过基于被单击的点从控件中获取行,您可能能够在grid_MouseDoubleClick处理程序中检索双击的行。例如,位置,这就是它在TreeView控件中的工作方式。快速一看,我没有看到控件是否有这样的方法。您可能想考虑使用DATAGIDVIEW,如果您没有使用此控件的特殊原因。 < P>我尝试了@ StestE76的代码,但不得不稍微调整它以在Windows嵌入式CE 6系统中工作。这是对我有用的东西
private void dataGrid1_DoubleClick(object sender, EventArgs e)
{
Point pt = dataGrid1.PointToClient(Control.MousePosition);
DataGrid.HitTestInfo info = dataGrid1.HitTest(pt.X, pt.Y);
int row;
int col;
if (info.Column < 0)
col = 0;
else
col = info.Column;
if (info.Row < 0)
row = 0;
else
row = info.Row;
object cellData = dataGrid1[row, col];
string cellString = "(null)";
if (cellData != null)
cellString = cellData.ToString();
MessageBox.Show(cellString, "Cell Contents");
}
如果有人想知道如何获取该值:System.Drawing.Point pt=dataGrid1.PointToClientCursor.Position;DataGrid.HitTestInfo info=dataGrid1.HitTestpt;int行;int col;如果信息列<0,则列=0;else col=信息列;如果信息行<0行=0;else行=信息行;此处的字符串=dataGrid1[行,列].ToString;MessageBox.Showhere;