Hi,
Here is a copy of a datagridtextbox column that I am working on. It
will automatically adjust the row height based to fit the data in it. There
is a bug when you add a record it sets the all rows back to the default
height.
Imports System.Reflecti on
Public Class MultiLineColumn
Inherits DataGridTextBox Column
Private mTxtAlign As HorizontalAlign ment
Private mDrawTxt As New StringFormat
Private mbAdjustHeight As Boolean = True
Private m_intPreEditHei ght As Integer
Private m_rownum As Integer
Dim WithEvents dg As DataGrid
Private arHeights As ArrayList
Dim WithEvents cm As CurrencyManager
Private Sub GetHeightList()
Dim mi As MethodInfo = dg.GetType().Ge tMethod("get_Da taGridRows",
BindingFlags.Fl attenHierarchy Or BindingFlags.Ig noreCase Or
BindingFlags.In stance Or BindingFlags.No nPublic Or BindingFlags.Pu blic Or
BindingFlags.St atic)
Dim dgra As Array = CType(mi.Invoke (Me.dg, Nothing), Array)
arHeights = New ArrayList
Dim dgRowHeight As Object
For Each dgRowHeight In dgra
If dgRowHeight.ToS tring().EndsWit h("DataGridRela tionshipRow") =
True Then
arHeights.Add(d gRowHeight)
End If
Next
End Sub
Public Sub New()
mTxtAlign = HorizontalAlign ment.Left
mDrawTxt.Alignm ent = StringAlignment .Near
'Me.ReadOnly = True
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(sou rce, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
Me.TextBox.Text Align = mTxtAlign
Me.TextBox.Mult iline = mbAdjustHeight
Dim iRows As Integer
If TypeOf dg.DataSource Is DataTable Then
iRows = DirectCast(dg.D ataSource, DataTable).Rows .Count
ElseIf TypeOf dg.DataSource Is DataView Then
iRows = DirectCast(dg.D ataSource, DataView).Count
ElseIf TypeOf dg.DataSource Is ArrayList Then
iRows = DirectCast(dg.D ataSource, ArrayList).Coun t
Else
iRows = DirectCast(dg.D ataSource, Collection).Cou nt
End If
Debug.WriteLine (iRows)
Debug.WriteLine (rowNum)
'If rowNum >= iRows Then
Debug.WriteLine ("New Row")
For x As Integer = 0 To arHeights.Count - 1
Dim pi As PropertyInfo =
arHeights(x).Ge tType().GetProp erty("Height")
Dim curHeight As Integer = pi.GetValue(arH eights(x), Nothing)
pi.SetValue(arH eights(x), curHeight, Nothing)
Next
Dim sz As Size = dg.Size
dg.Size = New Size(sz.Width - 1, sz.Height - 1)
dg.Size = sz
GetHeightList()
' End If
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.CurrencyM anager, 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.DataGridTabl eStyle.DataGrid
GetHeightList()
End If
cm = source
'clear the cell
g.FillRectangle (backBrush, bounds)
'draw the value
Dim s As String = Me.GetColumnVal ueAtRow([source],
rowNum).ToStrin g()
Dim r As New RectangleF(boun ds.X, bounds.Y, bounds.Width,
bounds.Height)
r.Inflate(0, -1)
' get the height column should be
Dim sDraw As SizeF = g.MeasureString (s, Me.TextBox.Font , Me.Width,
mDrawTxt)
Dim h As Integer = sDraw.Height + 15
If mbAdjustHeight Then
Try
Dim pi As PropertyInfo =
arHeights(rowNu m).GetType().Ge tProperty("Heig ht")
' get current height
Dim curHeight As Integer = pi.GetValue(arH eights(rowNum),
Nothing)
' adjust height
If h > curHeight Then
pi.SetValue(arH eights(rowNum), h, Nothing)
Dim sz As Size = dg.Size
dg.Size = New Size(sz.Width - 1, sz.Height - 1)
dg.Size = sz
End If
Catch
' something wrong leave default height
GetHeightList()
End Try
End If
g.DrawString(s, MyBase.TextBox. Font, foreBrush, r, mDrawTxt)
bPainted = True
End Sub
Public Property DataAlignment() As HorizontalAlign ment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlign ment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlign ment.Center Then
mDrawTxt.Alignm ent = StringAlignment .Center
ElseIf mTxtAlign = HorizontalAlign ment.Right Then
mDrawTxt.Alignm ent = StringAlignment .Far
Else
mDrawTxt.Alignm ent = StringAlignment .Near
End If
End Set
End Property
Public Property AutoAdjustHeigh t() As Boolean
Get
Return mbAdjustHeight
End Get
Set(ByVal Value As Boolean)
mbAdjustHeight = Value
Try
dg.Invalidate()
Catch
End Try
End Set
End Property
Private Sub cm_PositionChan ged(ByVal sender As Object, ByVal e As
System.EventArg s) Handles cm.PositionChan ged
Static intOld As Integer = 0
'If intOld > cm.Position Then
If cm.Count > DirectCast(dg.D ataSource, DataTable).Rows .Count Then
Debug.WriteLine ("New Row")
For x As Integer = 0 To arHeights.Count - 1
Dim pi As PropertyInfo =
arHeights(x).Ge tType().GetProp erty("Height")
Dim curHeight As Integer = pi.GetValue(arH eights(x),
Nothing)
pi.SetValue(arH eights(x), curHeight, Nothing)
Next
Dim sz As Size = dg.Size
dg.Size = New Size(sz.Width - 1, sz.Height - 1)
dg.Size = sz
End If
intOld = cm.Position
End Sub
End Class
Ken
-----------------------
"Tor Inge Rislaa" <to*******@risl aa.no> wrote in message
news:K3******** **********@news 4.e.nsc.no...
How to change row height in a DataGrid
I have DataGrid that is filled with data from a table in a DataSet. The
content of the cells is text of more than one line (as a note field). What
I
want is to set the height of the row based on the number of lines in it.
The
row height of each row in the table may be different from each other. Is
there a way to do this?
TIRislaa