是否有一个DataGrid一个双击事件?我试图用这个代码打开一个详细信息表单,当用户双击一行。C#的WinForm Datagrid中双击事件
http://www.codeproject.com/KB/grid/usingdatagrid.aspx
我试图通过双击在控制增加了,但它给dataGrid1_Navigate代替。
是否有一个DataGrid一个双击事件?我试图用这个代码打开一个详细信息表单,当用户双击一行。C#的WinForm Datagrid中双击事件
http://www.codeproject.com/KB/grid/usingdatagrid.aspx
我试图通过双击在控制增加了,但它给dataGrid1_Navigate代替。
当您在设计模式下双击某个控件时得到的结果是控件思想的设计者最常使用的事件,在这种情况下它是Navigate
。
但是,是的,这个控制有两种双击事件:
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)
{
}
}
然而,这两个事件的运行控制,当你双击任何地方,他们不直接给你什么是行的信息选择。您可以通过基于被点击的点(e.Location
)从控件中获取它,在grid_MouseDoubleClick
处理程序中双击行,这就是它在TreeView控件中的工作方式。乍一看,我没有看到控制是否有这种方法。如果您没有特别的理由使用此控件,则可能需要考虑使用DataGridView。
代码使用DataGrid对象,而不是一个DataGridView对象: – steve76 2010-10-17 21:03:45
听起来像是你需要一种方式来获得所有事件的列表对于给定的控制,而不是寻找默认的事件(这是VS给你当您双击在设计控制) 有这样做的几种方法:
一种方式选择网格。 然后点击活动图标,打开属性窗口为事件列表,然后doubel单击要strart编码事件的事件。
或者,切换到代码视图,在代码窗口左上角的对象下拉列表中选择网格,然后从事件列表中该控件的所有事件列表中选择所需事件(代码窗口的右上角)
我试了@ steve76的代码,但不得不微调它在Windows Embedded CE 6.0系统中工作。这是对我有用的东西。
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.PointToClient(Cursor.Position); DataGrid.HitTestInfo信息= dataGrid1.HitTest(PT); INT行; int col; if(info.Column <0)col = 0; else col = info.Column; if(info.Row <0)row = 0; else row = info.Row; 串此处= DataGrid1中[行,列]的ToString(); MessageBox.Show(here); – steve76 2010-10-17 23:53:19