tags:

views:

32

answers:

3

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

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

A: 

check out this link:

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

KhanZeeshan
The code uses a datagrid object, not a datagridview object:
steve76
A: 

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)

MikeG
A: 

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.

steinar
Here's how to get the value if anyone is wondering: System.Drawing.Point pt = dataGrid1.PointToClient(Cursor.Position); DataGrid.HitTestInfo info = dataGrid1.HitTest(pt); int row; int col; if (info.Column < 0) col = 0; else col = info.Column; if (info.Row < 0) row = 0; else row = info.Row; string here= dataGrid1[row, col].ToString(); MessageBox.Show(here);
steve76