How to programmatically Resizing DataGrid Columns in vb.net/c#

Public Class MyUtils_DataGrid
'Method: DataGridApplyAutomaticWidths
'Parameters
' DataGrid=DataGrid that is to be formatted.
' NumberOfRowsToScan=Number of data records to be scanned in order to compute the columns widths (Recommend 20).
' MaxPixelWidth=Maximum allowable pixel width of a column.
'
'Takes a DataGrid that should be already assigned a populated dataset,
'and formats the DataGrid so that the column widths reflect the widths of the data.
'This is accomplished by dynamically creating a single DataGridTableStyle and multiple DataGridTextBoxColumn(s).
'For each data column, the width of the column is set to the highest data width within the column.
'In order to reduce computation time where large datasets exist,
'a parameter exists to define how many rows are scanned.
'I would suggest that only the first 20 rows are scanned,
Public Shared Function DataGridApplyAutomaticWidths _
(ByVal DataGrid As System.Windows.Forms.DataGrid, ByVal NumberOfRowsToScan As Integer, ByVal MaxPixelWidth As Integer) _
As DataGridTableStyle
'Create graphics object for measuring widths.
Dim Graphics As Graphics = DataGrid.CreateGraphics()
'Define new table style.
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle()
Try
Dim DataTable As DataTable
'DataTable = DataGrid.DataSource.DataSet.Tables(0)
DataTable = DataGrid.DataSource.Tables(0)
'Can only scan rows if they exist.
NumberOfRowsToScan = System.Math.Min(NumberOfRowsToScan, DataTable.Rows.Count)
'Clear any existing table styles.
DataGrid.TableStyles.Clear()
'Use mapping name that is defined in the data source.
TableStyle.MappingName = DataTable.TableName
'Now create the column styles within the table style.
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
For Each Column In DataTable.Columns
ColumnStyle = New DataGridTextBoxColumn()
With ColumnStyle
.TextBox.Enabled = True
.HeaderText = Column.ColumnName
.MappingName = Column.ColumnName
'Set width to header text width.
Width = Graphics.MeasureString(.HeaderText, DataGrid.Font, MaxPixelWidth).Width
End With
'Change width, if data width is wider than header text width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim DataRow As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
DataRow = DataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width, Graphics.MeasureString(DataRow(Column.ColumnName), DataGrid.Font, MaxPixelWidth).Width)
End If
Next
ColumnStyle.Width = Width + 4
'Add the new column style to the table style.
TableStyle.GridColumnStyles.Add(ColumnStyle)
Next
'Add the new table style to the data grid.
DataGrid.TableStyles.Add(TableStyle)
Finally
Graphics.Dispose()
End Try
Return TableStyle
End Function

'This one allows a DataGridTableStyle to all ready exist on the table.
Public Shared Function DataGridApplyAutomaticWidths _
(ByVal DataGrid As System.Windows.Forms.DataGrid, ByVal TableStyle As DataGridTableStyle, _
ByVal NumberOfRowsToScan As Integer, ByVal MaxPixelWidth As Integer) _
As DataGridTableStyle
'Create graphics object for measuring widths.
Dim Graphics As Graphics = DataGrid.CreateGraphics()
Try
Dim DataTable As DataTable
'DataTable = DataGrid.DataSource.DataSet.Tables(0)
DataTable = DataGrid.DataSource.Tables(0)
'Can only scan rows if they exist.
NumberOfRowsToScan = System.Math.Min(NumberOfRowsToScan, DataTable.Rows.Count)
'Now loop through the columns and set the widths.
Dim Column As DataColumn
Dim ColumnStyle As DataGridColumnStyle
Dim Width As Integer
For Each Column In DataTable.Columns
ColumnStyle = TableStyle.GridColumnStyles(Column.ColumnName)
If Not ColumnStyle Is Nothing Then
'Set width to header text width.
Width = Graphics.MeasureString(ColumnStyle.HeaderText, DataGrid.Font, MaxPixelWidth).Width
'Change width, if data width is wider than header text width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim dr As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
dr = DataTable.Rows(iRow)
If Not IsDBNull(dr(Column.ColumnName)) Then
Width = System.Math.Max(Width, Graphics.MeasureString(dr(Column.ColumnName), DataGrid.Font, MaxPixelWidth).Width)
End If
Next
ColumnStyle.Width = Width + 4
End If
Next
Finally
Graphics.Dispose()
End Try
Return TableStyle
End Function

Public Shared Function GetNewDataGridTextBoxColumn(ByVal MappingName As String) As DataGridTextBoxColumn
Dim TextBoxColumn As DataGridTextBoxColumn = New DataGridTextBoxColumn()
With TextBoxColumn
.MappingName = MappingName
.HeaderText = MappingName
.ReadOnly = True
.Width = 200
End With
Return TextBoxColumn
End Function

Public Shared Function GetNewDataGridTextBoxColumn(ByVal MappingName As String, ByVal [ReadOnly] As Boolean) As DataGridTextBoxColumn
Dim TextBoxColumn As DataGridTextBoxColumn
TextBoxColumn = GetNewDataGridTextBoxColumn(MappingName)
TextBoxColumn.ReadOnly = [ReadOnly]
Return TextBoxColumn
End Function

End Class