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

double click on datagrid row
yaakov
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
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
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.
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
Tnx!
GaryStrader
Darren
krish_sathish
blabla
ShawnReed
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
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