Auto Ajust the Column width in a datagrid

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
 
K

Kevin Yu [MSFT]

Hi Gerry,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you would like to auto adjust grid
width according to the maximum length of text in the column. If there is
any misunderstanding, please feel free to let me know.

Generally, we can convert the length of text to pixels and compare between
each rows. Then set the maximum value for column width. Here I've written a
code snippet. HTH.

1. Bind the data to the datagrid:

Dim sConnectionString As String

sConnectionString = "Password=pass;User ID=user;" & _
"Initial Catalog=Northwind;" & _
"Data Source=(local)"

Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()

Dim daAuthors As _
New SqlDataAdapter("Select * From Customers", objConn)

Dim dsAccount As New DataSet("Northwind")
daAuthors.FillSchema(dsAccount, SchemaType.Source, "Customers")
daAuthors.Fill(dsAccount, "Customers")

EntryGrid.DataSource = dsAccount.Tables(0)
Dim ts As DataGridTableStyle
ts = New DataGridTableStyle
ts.MappingName = dsAccount.Tables(0).ToString()
EntryGrid.TableStyles.Add(ts)

2. For every column, call the function to set the width:

Dim columnlength As Integer = CType(EntryGrid.DataSource,
DataTable).Columns.Count

Dim j As Integer = 0

While j < columnlength
AutosizeCol(j)
j = j + 1
End While

3. The following is the function AutosizeCol to set the width. The function
will compare the width of the column name and the value and then set the
maximum value:

Function AutosizeCol(ByVal col As Integer)
Dim numRows As Integer = CType(EntryGrid.DataSource,
DataTable).Rows.Count

Dim width As Integer = CType(EntryGrid.DataSource,
DataTable).Columns(col).Caption.Length

Dim g As Graphics = Graphics.FromHwnd(EntryGrid.Handle)

Dim sf As StringFormat = New
StringFormat(StringFormat.GenericTypographic)

Dim size As SizeF

Dim i As Integer = 0
While i < numRows

size = g.MeasureString(EntryGrid(i, col).ToString(),
EntryGrid.Font, 500, sf)

If (size.Width > Width) Then
Width = size.Width
End If

i = i + 1

End While

g.Dispose()

EntryGrid.TableStyles(0).GridColumnStyles.Item(col).Width =
CType(width, Integer) + 4

End Function

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi Gerry,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top