This code will help to align the datagrid according to the contents (data) length in each columns.
Code:
SizeColumnsToContent(Me.DataGrid1, -1)
Public Sub SizeColumnsToContent(ByVal dataGrid As DataGrid, ByVal nRowsToScan As Integer)
' Create graphics object for measuring widths.
Dim Graphics As Graphics = DataGrid1.CreateGraphics
' Define new table style.
Dim tableStyle As DataGridTableStyle = New DataGridTableStyle
Try
Dim dataTable As DataTable = dataGrid.DataSource
If (-1 = nRowsToScan) Then
nRowsToScan = dataTable.Rows.Count
Else
' Can only scan rows if they exist.
nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count)
End If
' 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 textColumnStyle As DataGridTextBoxColumn
Dim iWidth As Integer
Dim iCurrCol As Integer
For iCurrCol = 0 To dataTable.Columns.Count - 8
Dim dataColumn As DataColumn = dataTable.Columns(iCurrCol)
textColumnStyle = New DataGridTextBoxColumn
textColumnStyle.TextBox.Enabled = True
textColumnStyle.HeaderText = dataColumn.ColumnName
textColumnStyle.MappingName = dataColumn.ColumnName
' Set width to header text width.
iWidth = (Graphics.MeasureString(textColumnStyle.HeaderText, dataGrid.Font).Width)
' Change width, if data width is wider than header text width.
' Check the width of the data in the first X rows.
Dim DataRow As DataRow
Dim iRow As Integer
For iRow = 0 To nRowsToScan - 1
DataRow = dataTable.Rows(iRow)
Dim iColWidth As Integer
iColWidth = (Graphics.MeasureString(DataRow.ItemArray(iCurrCol).ToString(), dataGrid.Font).Width)
iWidth = System.Math.Max(iWidth, iColWidth)
Next
textColumnStyle.Width = iWidth + 4
' Add the new column style to the table style.
tableStyle.GridColumnStyles.Add(textColumnStyle)
Next
' Now create the column styles within the table style.
Dim boolColumnStyle As DataGridBoolColumn
For iCurrCol = 2 To dataTable.Columns.Count - 1
Dim dataColumn As DataColumn = dataTable.Columns(iCurrCol)
boolColumnStyle = New DataGridBoolColumn
boolColumnStyle.AllowNull = False
boolColumnStyle.HeaderText = dataColumn.ColumnName
boolColumnStyle.MappingName = dataColumn.ColumnName
' Set width to header text width.
iWidth = (Graphics.MeasureString(boolColumnStyle.HeaderText, dataGrid.Font).Width)
' Change width, if data width is wider than header text width.
' Check the width of the data in the first X rows.
Dim DataRow As DataRow
Dim iRow As Integer
For iRow = 0 To nRowsToScan - 1
DataRow = dataTable.Rows(iRow)
Dim iColWidth As Integer
iColWidth = (Graphics.MeasureString(DataRow.ItemArray(iCurrCol).ToString(), dataGrid.Font).Width)
iWidth = System.Math.Max(iWidth, iColWidth)
Next
boolColumnStyle.Width = iWidth + 4
' Add the new column style to the table style.
tableStyle.GridColumnStyles.Add(boolColumnStyle)
Next
' Add the new table style to the data grid.
dataGrid.TableStyles.Add(tableStyle)
Catch e As Exception
Finally
Graphics.Dispose()
End Try
End Sub
hope this will help someone.
|