G
Gerry Viator
Hi again
I'm trying to ajust the width of columns in Datagrid based on the size of
the Column Names and eventually the Values as well?
The Code between the * is where I'm having the problem.
This is not working, I'm getting a Error
ShownDataTable.Columns.Clear()
Dim i As Integer
Dim CntColumn As New DataColumn("Entry #")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
ShownDataTable.Columns.Add(CntColumn)
Dim cols As DataColumnCollection
Dim iCol As Integer
cols = MainSavedDataTable.Columns
If cols.Contains(sEntryName) Then
iCol = (cols.IndexOf(sEntryName))
End If
For i = 1 To sHowManyColumnsPartOf
If sColumnLocation = 1 Then
Dim Strg As String =
MainSavedDataTable.Columns(iCol).Caption
Dim Colname As New DataColumn(Strg)
Colname.DataType = GetType(String)
ShownDataTable.Columns.Add(Colname)
Exit For
ElseIf sColumnLocation = 2 Then
Dim Strg As String = MainSavedDataTable.Columns(iCol -
1).Caption
Dim Colname As New DataColumn(Strg)
Colname.DataType = GetType(String)
ShownDataTable.Columns.Add(Colname)
Dim Strg2 As String =
MainSavedDataTable.Columns(iCol).Caption
Dim Colname2 As New DataColumn(Strg2)
Colname2.DataType = GetType(String)
ShownDataTable.Columns.Add(Colname2)
Exit For
End If
Next
'**************
Dim Graphics As Graphics = EntryGrid.CreateGraphics()
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle
Dim DataTable As DataTable = ShownDataTable
ShownDataTable = EntryGrid.DataSource.Tables(0)
Dim NumberOfRowsToScan As Integer =
System.Math.Min(NumberOfRowsToScan, ShownDataTable.Rows.Count)
EntryGrid.TableStyles.Clear()
TableStyle.MappingName = ShownDataTable.TableName
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
Dim MaxPixelWidth As Integer = 120
For Each Column In ShownDataTable.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, EntryGrid.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 = ShownDataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width,
Graphics.MeasureString(DataRow(Column.ColumnName), EntryGrid.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.
EntryGrid.TableStyles.Add(TableStyle)
Graphics.Dispose()
'**********
EntryGrid.DataSource = ShownDataTable
Thanks
Gerry
I'm trying to ajust the width of columns in Datagrid based on the size of
the Column Names and eventually the Values as well?
The Code between the * is where I'm having the problem.
This is not working, I'm getting a Error
ShownDataTable.Columns.Clear()
Dim i As Integer
Dim CntColumn As New DataColumn("Entry #")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
ShownDataTable.Columns.Add(CntColumn)
Dim cols As DataColumnCollection
Dim iCol As Integer
cols = MainSavedDataTable.Columns
If cols.Contains(sEntryName) Then
iCol = (cols.IndexOf(sEntryName))
End If
For i = 1 To sHowManyColumnsPartOf
If sColumnLocation = 1 Then
Dim Strg As String =
MainSavedDataTable.Columns(iCol).Caption
Dim Colname As New DataColumn(Strg)
Colname.DataType = GetType(String)
ShownDataTable.Columns.Add(Colname)
Exit For
ElseIf sColumnLocation = 2 Then
Dim Strg As String = MainSavedDataTable.Columns(iCol -
1).Caption
Dim Colname As New DataColumn(Strg)
Colname.DataType = GetType(String)
ShownDataTable.Columns.Add(Colname)
Dim Strg2 As String =
MainSavedDataTable.Columns(iCol).Caption
Dim Colname2 As New DataColumn(Strg2)
Colname2.DataType = GetType(String)
ShownDataTable.Columns.Add(Colname2)
Exit For
End If
Next
'**************
Dim Graphics As Graphics = EntryGrid.CreateGraphics()
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle
Dim DataTable As DataTable = ShownDataTable
ShownDataTable = EntryGrid.DataSource.Tables(0)
Dim NumberOfRowsToScan As Integer =
System.Math.Min(NumberOfRowsToScan, ShownDataTable.Rows.Count)
EntryGrid.TableStyles.Clear()
TableStyle.MappingName = ShownDataTable.TableName
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
Dim MaxPixelWidth As Integer = 120
For Each Column In ShownDataTable.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, EntryGrid.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 = ShownDataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width,
Graphics.MeasureString(DataRow(Column.ColumnName), EntryGrid.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.
EntryGrid.TableStyles.Add(TableStyle)
Graphics.Dispose()
'**********
EntryGrid.DataSource = ShownDataTable
Thanks
Gerry