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