开发者

C# WinForm Datagrid doubleclick event

Is there a doubleclick e开发者_如何学运维vent 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

I tried adding it by doubleclicking on the control, but it gives dataGrid1_Navigate instead.


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.

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. 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.


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:

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)


I tried @steve76's code, but had to tweak it slightly to work in a Windows Embedded CE 6.0 system. 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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜