Adjustment on datagrid column width

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
Is it possible to adjust width according to the length of data, because some some column is sufficient with narrow space whereas some may need more space to display lengthy data?
 
retkehing said:
Is it possible to adjust width according to the length of data, because some some column is sufficient with narrow space whereas some may need more space to display lengthy data?

VB.NET:
Public Sub AutoSizeCol(ByVal col As Integer)

Dim width As Single
width = 0
Dim numRows As Integer
numRows = CType(dg.DataSource, DataTable).Rows.Count
Dim g As Graphics
g = Graphics.FromHwnd(dg.Handle)
Dim sf As StringFormat
sf = New StringFormat(StringFormat.GenericTypographic)
Dim size As SizeF
Dim i As Integer
i = 0

Do While (i < numRows)
size = g.MeasureString(dg(i, col).ToString, dg.Font, 500, sf)
If (size.Width > width) Then
width = size.Width
End If
i = (i + 1)

Loop
g.Dispose()
dg.TableStyles("customers").GridColumnStyles(col).Width =
CType(width, Integer)

End Sub
 
Back
Top