How can I change the font used in a grid cell on a cell by cell or row by row basis
One way to do this is to use a derived columnstyle, override the Paint method and do the text drawing yourself, using whatever font or colors you like. If you add an event to your derived column style that is fired immediately before the text is drawn, and use the event args to get the font and color information, you can let the event handler completely determine the look of any cell. The attached samples (C#, VB) use this technique to create a grid that looks like the grid in the picture. Both color and font varies on a cell basis or row basis in this picture.
Can I play audio and video files using .NET
You cannot directly do this using .NET. However, there are some wrapper classes available at http://www.mentalis.org/soft/class.qpx?id=1 that allow you to do just this. You can download the latest version of these classes directly from the site. We have packaged these classes as a library and added a simple Winforms sample and made these available here. This library basically uses the Media Control Interface (MCI).
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