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] "Douglas Buchanan" <dbuchanan52@hotmail.com> wrote in message
news:7217f9ea.0411062331.37d35d20@posting.google.com...
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