How do I catch a doubleclick in my datagrid

The problem is that the first click of a double click may be caught by the datagrid (and used to activate the cell) while the second click goes to the TextBox for the columnstyle object. This means the TextBox thinks this is a singleclick, and does not fire its doubleclick event. One solution is to mark the time of the click caught by the datagrid. Then look at this time in the TextBox’s mousedown handler to see if in fact the single click being looked at by the TextBox is part of a double click. You can download a sample (C#, VB) that illustrates how this might be done.

How can I auto-adjust keyboard input? For example, make typing 12312002 be taken as a valid date, 12/31/2002.

One possible solution is that as you move off the cell, you get the typed value, and modify it before it is passed onto the DataGrid for its standard processing. One problem is that there are several ways to leave the cell, and you would want to handle each (depending upon your needs). You can leave the cell by clicking on another cell, by tabbing off the cell, by arrowing off the cell, and by pressing Enter on an edited cell. Another problem is that you want to catch these actions early enough to make a change in the typed value that can be passed onto the DataGrid itself. To catch the typed entries before they are handed off to the DataGrid, you have to gain access to the DataGridTextBoxColumn’s embedded TextBox. This requires that your DataGrid either has explicitly had these columns styles added to it, or that you minimally add a DataGridTableStyle to your DataGrid so the Framework generates ColumnsStyle objects for you. To solve these problems, you can derive the dataGrid and override OnMouseDown, OnProcessDialogKey, and OnProcessPreviewKey. The last override will handle both the arrowing off the cell and pressing Enter on an edited cell. You can download a sample (C#, VB.NET) that implements this technique.

How can I format columns in my DataGrid without explicitly adding DataGridColumnStyles

To format column output, you do not have to explicitly add DataGridColumns for each column provided you do add a DataGridTableStyle to your DataGrid prior to setting the DataSource property. When you set the DataSource property with a DataGrid that has a tablestyle with an empty columnstyle collection, the framework generates default columnstyle objects for each column in the datasource. You can then access these columnstyles directly and set properties in them such as Format, HeaderText and Width. Download working samples here (VB.NET, C#). Dim dataTableName As String = ”theTable” ’add a tablestyle to the grid so there will be custom columnstyles available ’ after the datasource has been set…. Dim ts As New DataGridTableStyle() ts.MappingName = dataTableName Me.dataGrid1.TableStyles.Add(ts) Me.dataGrid1.DataSource = GetTheTable(dataTableName) ’now default customcolumnstyles have been created, so we can use them to set properties Dim dgtbc As DataGridTextBoxColumn ’format the int dgtbc = dataGrid1.TableStyles(0).GridColumnStyles(0) If Not (dgtbc Is Nothing) Then dgtbc.Format = ”n0” End If ’format the double dgtbc = dataGrid1.TableStyles(0).GridColumnStyles(1) ’ If Not (dgtbc Is Nothing) Then dgtbc.Format = ”f3” ’ 0r ”#.000”; End If ’format the double as currency dgtbc = dataGrid1.TableStyles(0).GridColumnStyles(2) ’ If Not (dgtbc Is Nothing) Then dgtbc.Format = ”c4” End If ’format the date dgtbc = dataGrid1.TableStyles(0).GridColumnStyles(3) ’ If Not (dgtbc Is Nothing) Then dgtbc.Format = ”d” ’ or ”g” or ”u” or whatever format you want to see dgtbc.Width = 100 ’size it End If

I have two forms. How can I access a textbox on one form from the other form

One way to do this is to make the TextBox either a public property or a public field. Then you will be able to access it through the instance of its parent form. So, if TextBox1 is a public member of FormA and myFormA is an instance of FormA, then you can use code such as [VB.NET] Dim strValue as String = myFormA.TextBox1.Text [C#] string strValue = myFormA.TextBox1.Text; anywhere myFormA is known. Here is a VB project illustrating this technique.

How can I autosize a column in my datagrid

One way to do this is to use MeasureString to compute the size of the text in each cell, and then take the maximum value. Below is a code snippet that does this. It assumes your datagrid is bound to a datatable. You can download a full working sample. (C#,VB). public void AutoSizeCol(int col) { float width = 0; int numRows = ((DataTable) dataGrid1.DataSource).Rows.Count; Graphics g = Graphics.FromHwnd(dataGrid1.Handle); StringFormat sf = new StringFormat(StringFormat.GenericTypographic); SizeF size; for(int i = 0; i < numRows; ++ i) { size = g.MeasureString(dataGrid1[i, col].ToString(), dataGrid1.Font, 500, sf); if(size.Width > width) width = size.Width; } g.Dispose(); dataGrid1.TableStyles[”customers”].GridColumnStyles[col].Width = (int) width + 8; // 8 is for leading and trailing padding }