Groups | Blog | Home
all groups > vb.net controls > november 2004 >

vb.net controls : Incorrect Auto-Size of Columns


dbuchanan52 NO[at]SPAM hotmail.com
11/6/2004 11:31:21 PM
This code yeilds the wrong column width for one of the columns.
It may have to do with the fact that the column holds integers rather
than text.

The results when values for that column are only single digits is a
column that is so narrow that only half of any of the digits are
shown. The results when row values for that column are three digits in
length is a column so narrow that only two of the digits are shown.

All other columns are an appropriate width.

=== Here are pertinent portions of code ===

Private Sub btnAutoSize_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnAutoSize.Click
AutoSizeTable()

End Sub

Public Sub AutoSizeTable()

Dim numCols As Integer
numCols = Me.Ds1.tblGUID.Columns.Count

Dim i As Integer
i = 0
Do While (i < numCols)
AutoSizeCol(i)
i = (i + 1)
Loop

End Sub

Public Sub AutoSizeCol(ByVal col As Integer)

Dim width As Single
width = 0

Dim numRows As Integer
numRows = Me.Ds1.tblGUID.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)
' Get the size of this column in this row
size = g.MeasureString(dg(i, col).ToString, dg.Font, 500,
sf)

' Find the widest row of the column
If (size.Width > width) Then
width = size.Width
End If
i = (i + 1)

Loop
g.Dispose()

' Assign the width
dg.TableStyles("tblGUID").GridColumnStyles(col).Width =
CType(width, Integer)

Ken Tucker [MVP]
11/7/2004 6:48:10 AM
Hi,

Here is a custom column style i wrote which automatically adjusts
the column width to fit the text. Use this in your tablestyle instead of a
datagridtextbox column.

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms



Public Class AutoWidthColumn

Inherits DataGridTextBoxColumn

Dim dg As DataGrid

Dim cm As CurrencyManager



Private Sub SetWidth()

Dim intWidth As Integer = Me.Width

Dim intRow As Integer

Dim sz As Size = dg.Size

Dim g As Graphics = dg.CreateGraphics

For intRow = 0 To cm.Count - 1

Dim s As String

Dim i As Integer

s = Me.GetColumnValueAtRow(cm, intRow).ToString()

i = CInt(g.MeasureString(s, Me.TextBox.Font).Width + 10)

If i > intWidth Then

Debug.WriteLine(i.ToString)

intWidth = i

End If

Next

Me.Width = intWidth

g.Dispose()

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

If Not bPainted Then

dg = Me.DataGridTableStyle.DataGrid

cm = source

SetWidth()

bPainted = True

Me.TextBox.Multiline = False

AddHandler Me.TextBox.Validating, AddressOf TextValidating

End If

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

End Sub

Private Sub TextValidating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)

Dim s As String = Me.TextBox.Text

Dim g As Graphics = dg.CreateGraphics

Dim iWidth As Integer = CInt(g.MeasureString(s, dg.Font).Width + 10)

g.Dispose()

If iWidth > Me.Width Then

Me.Width = iWidth

Else

SetWidth()

End If

End Sub



End Class



Ken

------------------------

[quoted text, click to view]
This code yeilds the wrong column width for one of the columns.
It may have to do with the fact that the column holds integers rather
than text.

The results when values for that column are only single digits is a
column that is so narrow that only half of any of the digits are
shown. The results when row values for that column are three digits in
length is a column so narrow that only two of the digits are shown.

All other columns are an appropriate width.

=== Here are pertinent portions of code ===

Private Sub btnAutoSize_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnAutoSize.Click
AutoSizeTable()

End Sub

Public Sub AutoSizeTable()

Dim numCols As Integer
numCols = Me.Ds1.tblGUID.Columns.Count

Dim i As Integer
i = 0
Do While (i < numCols)
AutoSizeCol(i)
i = (i + 1)
Loop

End Sub

Public Sub AutoSizeCol(ByVal col As Integer)

Dim width As Single
width = 0

Dim numRows As Integer
numRows = Me.Ds1.tblGUID.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)
' Get the size of this column in this row
size = g.MeasureString(dg(i, col).ToString, dg.Font, 500,
sf)

' Find the widest row of the column
If (size.Width > width) Then
width = size.Width
End If
i = (i + 1)

Loop
g.Dispose()

' Assign the width
dg.TableStyles("tblGUID").GridColumnStyles(col).Width =
CType(width, Integer)

End Sub

dbuchanan52 NO[at]SPAM hotmail.com
11/9/2004 3:35:03 PM
Ken,

How do I use it? I tried this...

I put your code into a new class.

Then I changed my code to this:

Dim tableStyle As AutoWidthColumn
tableStyle = New AutoWidthColumn

but I get an error at this statement:

dg.TableStyles.Add(tableStyle)

It tells me that...

Value of type 'guidTable.AutoWidthColumn' cannot be converted to
'System.Windows.Forms.DataGridTableStyle'.

See my original code to find where these statements are.

Asside from me guessing. How do I implement your code.

--Doug

[quoted text, click to view]
AddThis Social Bookmark Button