double click on datagrid row

Hi, can anybody tell me how to capture the double click event in a datagrid. What i want to do is when user double click on any cell a row in the datagrid, a new form pop out showing the details of that particular row use for updating purpose. I do not want the mouse cursor to become a text pointer when the user click on the cell.
Anybody know how to do it  Or know where can i get the code to solve the arising problem. Any answers to this question will be appreciated. 
Urgent, thanks.

:-)





Answer this question

double click on datagrid row

  • yaakov

    I have produced similar forms with a parent "Products" form with a view of all products in datagrid, and a child "Product" form for editing the product details. 

    There may be a better way of doing it, but this seems to work:

    -On the parent form I have attached to the double click event in the usual way:

    this.dgProducts.DoubleClick += new System.EventHandler(this.dgProducts_DoubleClick);

    -Then in the double click event I have created the Product form and passed the dataset and the ProductId obtained from the current row index (and a specified column known to contain the ProductId), into public properties on the Product form.

    private void dgProducts_DoubleClick(object sender, System.EventArgs e)
    {
    Products.ProductForm productForm = new ProductForm();


    int productId = Convert.ToInt32(
    dgProducts[dgProducts.CurrentRowIndex,0]);

    productForm.ProductId = productId;
    productForm.Products=_products;

    Cursor.Current = Cursors.WaitCursor;
    productForm.ShowDialog(this);
    Cursor.Current = Cursors.Default;
    dgProducts.Refresh();
    }


    Then on the child form I filter the default dataview of the dataset that I have passed:

    DataView dv = _products.Tables["Products"].DefaultView;
    dv.RowFilter="ProductId = '" + _productId.ToString() +  "'";

    And bind the dataset to the form in the usual way.

    Hope this helps,

    Regards

    Darren

  • Sirkku Willie MSFT

    Awesome code man!!  What a great solution for a view-only datagrid.

    I recomend a procedure to do this:

    Private Sub RemoveTextBoxes(ByVal DataGridObj as DataGrid)
    Dim al As New ArrayList() 
    Dim c As Control 
    For Each c In DataGridObj.Controls 
    If c.GetType() Is GetType(VScrollBar) OrElse _ 
    c.GetType() Is GetType(HScrollBar) Then 
    al.Add(c) 
    End If 
    Next c 
    DataGridObj.Controls.Clear() 
    DataGridObj.Controls.AddRange(CType(al.ToArray(GetType(Control)), Control())) 
    End Sub

    Call this sub with reference to your grid whenever you bind, and in the Grid's DataGrid.Navigate event!

  • mooremedia

    A somewhat simpler solution would be to create a derived class from DataGridTextBoxColumn, then override the Edit  method and leave it empty.
    After that, change all the definitions from DataGridTextBoxColumn to the new DataGridLabelColumn.
    All this does is disable the edit functionality thus making it seem to be a Label instead of textbox.
    Tried it with c# and works fine

    #vb code
    Public Class DataGridLabelColumn
        Inherits DataGridTextBoxColumn

        Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
    ' dont call the baseclass so no editing done...
        End Sub

    End Class

    #c# code

    public class DataGridLabelColumn : DataGridTextBoxColumn
    {
    protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) 
    {
    // dont call the baseclass so no editing done...
    }
    }

  • Nick__A.

    removing components from datagrid:

          Dim al As New ArrayList()
          Dim c As Control
          For Each c In Me.dGrid.Controls
            If c.GetType() Is GetType(VScrollBar) OrElse _
                c.GetType() Is GetType(HScrollBar) Then
              al.Add(c)
            End If
          Next c
          Me.dGrid.Controls.Clear()
          Me.dGrid.Controls.AddRange(CType(al.ToArray(GetType(Control)), Control()))

  • DianeWilson

    Oh, well, I didn't foind "removing the text box controls from a datagrid. " but I've found a solution here: http://www.syncfusion.com/faq/winforms/search/689.asp

    Tnx!

  • GaryStrader

    by the way, you need to double-click on the side of the record in the datagrid, and not in the actual cell for the double-click event to fire.

    Darren

  • krish_sathish

    Shoot! In my book and with my cold dead hands this won't work - it needs to be a DOUBLE-CLICK event folks.
  • blabla

    I'm sorry but it seems I can't find anything useful for my task: I have a Windows Form and need to capture the DoubleClick event on a row of a datagrid (populated by a dataview after a query bla bla :^). Since after the first click You enter in some cell, the datagrid "looses" the second click. Could someone please put a bit of code in here  Thanks a lot!
  • ShawnReed

    The Windows Forms FAQ at www.syncfusion.com includes a section on the datagrid and a tip about removing the text box controls from a datagrid.

    Once the text boxes have been removed, they will not grab the focus on the first click, and you can therefore handle a double click on the grid itself.  Use HitTestInfo to find which row was clicked.

    The lack of text boxes in the grid will not be a problem for you as you are using a seperate form for data entry.

  • DanielMcN

    Hope this will solve your problem:

    Declare a varibale at the class level:
    private DateTime gridClickTime ;

    Use the MouseDown and MouseUp events for the DataGridTextBoxColumn .
    (This has to be done at the time of databinding.)

    //....
    dataGridTextBoxColumn .TextBox.MouseDown += new  MouseEventHandler(this.DataGridTextBox_MouseDown);
    dataGridTextBoxColumn .TextBox.MouseUp+= new  MouseEventHandler(this.DataGridTextBox_MouseUp);
    //add the dataGridTextBoxColumn  to the table style.

    //Implement the event handlers
    private void DataGridTextBox_MouseDown(object sender, MouseEventArgs e)  


    if(DateTime.Now <= gridClickTime.AddMilliseconds(SystemInformation.DoubleClickTime))
    {
    //your code

    }

    }
    private void DataGridTextBox_MouseUp(object sender, MouseEventArgs e)
    {
    gridClickTime = DateTime.Now;

    }

    regards,
    manik

  • double click on datagrid row