繁体   English   中英

C# WinForm Datagrid 双击事件

[英]C# WinForm Datagrid doubleclick event

数据网格是否有双击事件? 当用户双击一行时,我试图使用此代码打开详细信息表单。

http://www.codeproject.com/KB/grid/usingdatagrid.aspx

我尝试通过双击控件来添加它,但它提供了 dataGrid1_Navigate。

我尝试了@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");
}

也许您可以使用DataGridView.CellContentDoubleClick事件。

示例:

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" );
}

如果这不是您要查找的内容,您可以在参考资料中找到其他事件:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_events.aspx

听起来您需要一种方法来获取给定控件的所有事件的列表,而不是查找默认事件(这是在设计器中双击控件时VS 为您提供的)有几种方法可以做到这一点:

一种方式 选择网格。 然后单击事件图标将属性窗口变成事件列表,然后双击要开始对事件进行编码的事件。

或者,切换到代码视图,在代码窗口左上角的对象下拉列表中选择网格,然后从事件列表中该控件的所有事件列表中选择您想要的事件(右上角的代码窗口)

在设计模式下双击控件时,您得到的是控件设计者认为最常用的事件,在本例中为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。

暂无
暂无

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

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