Public Class ConfigureTableStyle 'Having had no success configuring table styles in a simple fashion, this class is being written to solve this problem 'once and for all. It will accept as input a datatable and datagrid. A new tablestyle will be created internally. It 'will then read the columns and rows in the datatable and will setup the width and datatype for each column 'in the tablestyle. It will also configure the column and table mapping for the datagrid tablestyle and add the 'tablestyle to the datagrid. When this class returns the datagrid to the calling routine it is hoped that the only task 'remaining is to display the datagrid. ' 'It is believed that before passing to this routine, the datatable should be bound to the datagrid. ' 10-30-2005 ' Private tsTableStyle As New DataGridTableStyle Private dtDataTable As New DataTable Private dgDataGrid As New DataGrid Private blnErrorOccurred As Boolean Private strErrorMessage As String Public WriteOnly Property Datatable() As datatable Set(ByVal Value As datatable) dtDataTable = Value End Set End Property Public WriteOnly Property Datagrid() As datagrid Set(ByVal Value As datagrid) dgDataGrid = Value End Set End Property Public ReadOnly Property Tablestyle() As DataGridTableStyle Get Return tsTableStyle End Get End Property Public ReadOnly Property ErrorOccured() As Boolean Get Return blnErrorOccurred End Get End Property Public ReadOnly Property ErrorMessage() As String Get Return strErrorMessage End Get End Property Public Sub FillTableStyle() Dim arlFieldLengths As New ArrayList Dim intColumnCounter As Integer If dtDataTable.Rows.Count = 0 Then blnErrorOccurred = True strErrorMessage = "FillTableStyle routine was called with no datatable as input!" Exit Sub End If If dgDataGrid Is Nothing Then blnErrorOccurred = True strErrorMessage = "FillTableStyle routine was called with no datagrid as input!" Exit Sub End If tsTableStyle.MappingName = dtDataTable.TableName CalculatedLongestFields(dtDataTable, arlFieldLengths) For intColumnCounter = 0 To dtDataTable.Columns.Count - 1 If dtDataTable.Columns(intColumnCounter).GetType.ToString = "System.boolean" Then Dim datacolumn As New DataGridBoolColumn datacolumn.MappingName = dtDataTable.Columns(intColumnCounter).ColumnName datacolumn.Width = arlFieldLengths(intColumnCounter) datacolumn.HeaderText = dtDataTable.Columns(intColumnCounter).ColumnName datacolumn.Alignment = HorizontalAlignment.Center tsTableStyle.GridColumnStyles.Add(datacolumn) Else Dim datacolumn As New DataGridTextBoxColumn datacolumn.MappingName = dtDataTable.Columns(intColumnCounter).ColumnName datacolumn.Width = arlFieldLengths(intColumnCounter) datacolumn.HeaderText = dtDataTable.Columns(intColumnCounter).ColumnName tsTableStyle.GridColumnStyles.Add(datacolumn) End If Next End Sub Private Function CalculatedLongestFields(ByVal DataTable As Datatable, ByRef FieldLengths As ArrayList) As Boolean 'This function will accept as input a datatable. We wish to calculate the longest field of each 'column, using the font size of the form datagrid as a guide. 'This function was created using an algorithm from the Web, the assumption is that the 'integer relates to pixels on the screen 'If the function executes without errors we return true Dim maxlength As Integer = 0 Dim g As Graphics = dgDataGrid.CreateGraphics() ' Take width of one blank space and add to the new width of the Column. Dim offset As Integer = Convert.ToInt32(Math.Ceiling(g.MeasureString("W", dgDataGrid.Font).Width)) Dim intColumnCounter As Integer Dim intRowCounter As Integer Dim strCurrentFieldString As String Dim intCurrentFieldLength As Integer For intColumnCounter = 0 To DataTable.Columns.Count - 1 strCurrentFieldString = DataTable.Columns(intColumnCounter).ColumnName 'Start maxlength as the length needed for the column heading maxlength = Convert.ToInt32(Math.Ceiling(g.MeasureString(strCurrentFieldString, dgDataGrid.Font).Width)) For intRowCounter = 0 To DataTable.Rows.Count - 1 Try 'if we have a null value we cannot convert to a string so the length is 0 If DataTable.Rows(intRowCounter).Item(intColumnCounter).GetType.ToString = "System.DBNull" Then intCurrentFieldLength = 0 Else strCurrentFieldString = CStr(DataTable.Rows(intRowCounter).Item(intColumnCounter)) intCurrentFieldLength = Convert.ToInt32(Math.Ceiling(g.MeasureString(strCurrentFieldString, dgDataGrid.Font).Width)) End If Catch ex As Exception ' MsgBox("Error calculating field lengths for dataset, error from Windows is:" & vbNewLine & _ ' ex.Message) Return False Exit Function End Try If (intCurrentFieldLength > maxlength) Then maxlength = intCurrentFieldLength End If Next maxlength += offset FieldLengths.Add(maxlength) Next Return True End Function End Class