简体   繁体   English

C# WinForm Datagrid 双击事件

[英]C# WinForm Datagrid doubleclick event

Is there a doubleclick event for a datagrid?数据网格是否有双击事件? I'm trying to use this code to open a details form when the user doubleclicks on a row.当用户双击一行时,我试图使用此代码打开详细信息表单。

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

I tried adding it by doubleclicking on the control, but it gives dataGrid1_Navigate instead.我尝试通过双击控件来添加它,但它提供了 dataGrid1_Navigate。

I tried @steve76's code, but had to tweak it slightly to work in a Windows Embedded CE 6.0 system.我尝试了@steve76 的代码,但必须稍微调整它才能在 Windows Embedded CE 6.0 系统中工作。 Here is what worked for me.这对我有用。

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

Perhaps you can use the DataGridView.CellContentDoubleClick event.也许您可以使用DataGridView.CellContentDoubleClick事件。

Example:示例:

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

If that is not what you are looking for, you can find other events in the reference:如果这不是您要查找的内容,您可以在参考资料中找到其他事件:

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

Sounds like you need a way to get a list of all the events for a given control, rather than finding the default event (which is what VS gives you when you double click a control in the designer) There are a few ways of doing this:听起来您需要一种方法来获取给定控件的所有事件的列表,而不是查找默认事件(这是在设计器中双击控件时VS 为您提供的)有几种方法可以做到这一点:

One way Select the grid.一种方式 选择网格。 Then click the events icon to turn the properties window into a list of events, then doubel click the event you want to strart coding the event.然后单击事件图标将属性窗口变成事件列表,然后双击要开始对事件进行编码的事件。

Alternatively, switch to code view, select the grid in the drop down list of objects at the top left of the code window, then select the event you want from the list of all the events for that control in the event list (top right of the code window)或者,切换到代码视图,在代码窗口左上角的对象下拉列表中选择网格,然后从事件列表中该控件的所有事件列表中选择您想要的事件(右上角的代码窗口)

What you get when you double click a control in design mode is the event the designers of the control thought would be the most used, in this case it's Navigate .在设计模式下双击控件时,您得到的是控件设计者认为最常用的事件,在本例中为Navigate

But yes, this control has two double click events:但是是的,这个控件有两个双击事件:

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)
    {            
    }
}

However, both of these events run when you double click anywhere on the control and they don't directly give you information on what row was selected.但是,当您双击控件上的任意位置时,这两个事件都会运行,并且它们不会直接为您提供有关所选行的信息。 You might be able to retrieve the row double clicked in the grid_MouseDoubleClick handler by getting it from the control based on the point being clicked ( e.Location ), that's how it works in the TreeView control for example.您可以根据单击的点 ( e.Location ) 从控件中获取在grid_MouseDoubleClick处理程序中双击的,这就是它在 TreeView 控件中的工作方式。 At a quick glance I didn't see if the control has such a method.乍一看,我没有看到控件是否有这样的方法。 You might want to consider using DataGridView instead, if you don't have a particular reason to use this control.如果您没有使用此控件的特殊原因,您可能需要考虑改用 DataGridView。

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

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