Public Sub AutoSizeCol(ByRef dg As DataGrid, _ Optional ByVal col As Integer = 0, _ Optional ByVal iTable As Integer = 0) Dim swidth As Single = 0 Dim iPadding As Int16 = 8 ' Set the padding size Dim g As Graphics = Graphics.FromHwnd(dg.Handle) ' Get a handle to the grid Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic) ' We need this for the measurements Dim size As SizeF ' Stores an ordered pair of floating-point numbers Try Dim i As Integer For i = 0 To 5000000 ' Loop till EOF then fall out to finally size = g.MeasureString(dg(i, col).ToString(), dg.Font, 500, sf) ' Get the 'size' of the text If size.Width > swidth Then ' If this is the biggest save it swidth = size.Width ' Keep collecting the largest size End If Next Catch ' Just run like -while not EOF- Finally ' Finally :) g.Dispose() ' House cleaning dg.TableStyles(iTable).GridColumnStyles(col).Width = swidth + iPadding ' Padding End Try End Sub* :( There should be access to the internal method of the datagrid to do this. It is available to the control when you dblclick the edge of the column header without this code.
"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams