By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,686 Members | 1,903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,686 IT Pros & Developers. It's quick & easy.

How to change row height in a DataGrid

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Here is a copy of a datagridtextboxcolumn 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.Reflection

Public Class MultiLineColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment
Private mDrawTxt As New StringFormat
Private mbAdjustHeight As Boolean = True
Private m_intPreEditHeight 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().GetMethod("get_DataGridRows",
BindingFlags.FlattenHierarchy Or BindingFlags.IgnoreCase Or
BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or
BindingFlags.Static)

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.ToString().EndsWith("DataGridRelations hipRow") =
True Then
arHeights.Add(dgRowHeight)
End If
Next
End Sub

Public Sub New()
mTxtAlign = HorizontalAlignment.Left
mDrawTxt.Alignment = StringAlignment.Near
'Me.ReadOnly = True
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
Me.TextBox.TextAlign = mTxtAlign
Me.TextBox.Multiline = mbAdjustHeight

Dim iRows As Integer
If TypeOf dg.DataSource Is DataTable Then
iRows = DirectCast(dg.DataSource, DataTable).Rows.Count
ElseIf TypeOf dg.DataSource Is DataView Then
iRows = DirectCast(dg.DataSource, DataView).Count
ElseIf TypeOf dg.DataSource Is ArrayList Then
iRows = DirectCast(dg.DataSource, ArrayList).Count
Else
iRows = DirectCast(dg.DataSource, Collection).Count
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).GetType().GetProperty("Height")
Dim curHeight As Integer = pi.GetValue(arHeights(x), Nothing)
pi.SetValue(arHeights(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.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
GetHeightList()
End If

cm = source

'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String = Me.GetColumnValueAtRow([source],
rowNum).ToString()

Dim r As New RectangleF(bounds.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(rowNum).GetType().GetProperty("Height")
' get current height
Dim curHeight As Integer = pi.GetValue(arHeights(rowNum),
Nothing)

' adjust height

If h > curHeight Then
pi.SetValue(arHeights(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 HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

Public Property AutoAdjustHeight() 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_PositionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.PositionChanged
Static intOld As Integer = 0
'If intOld > cm.Position Then
If cm.Count > DirectCast(dg.DataSource, DataTable).Rows.Count Then
Debug.WriteLine("New Row")

For x As Integer = 0 To arHeights.Count - 1
Dim pi As PropertyInfo =
arHeights(x).GetType().GetProperty("Height")
Dim curHeight As Integer = pi.GetValue(arHeights(x),
Nothing)
pi.SetValue(arHeights(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*******@rislaa.no> wrote in message
news:K3******************@news4.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

Nov 20 '05 #2

P: n/a
Hi,

Here is a copy of a datagridtextboxcolumn 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.Reflection

Public Class MultiLineColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment
Private mDrawTxt As New StringFormat
Private mbAdjustHeight As Boolean = True
Private m_intPreEditHeight 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().GetMethod("get_DataGridRows",
BindingFlags.FlattenHierarchy Or BindingFlags.IgnoreCase Or
BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or
BindingFlags.Static)

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.ToString().EndsWith("DataGridRelations hipRow") =
True Then
arHeights.Add(dgRowHeight)
End If
Next
End Sub

Public Sub New()
mTxtAlign = HorizontalAlignment.Left
mDrawTxt.Alignment = StringAlignment.Near
'Me.ReadOnly = True
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
Me.TextBox.TextAlign = mTxtAlign
Me.TextBox.Multiline = mbAdjustHeight

Dim iRows As Integer
If TypeOf dg.DataSource Is DataTable Then
iRows = DirectCast(dg.DataSource, DataTable).Rows.Count
ElseIf TypeOf dg.DataSource Is DataView Then
iRows = DirectCast(dg.DataSource, DataView).Count
ElseIf TypeOf dg.DataSource Is ArrayList Then
iRows = DirectCast(dg.DataSource, ArrayList).Count
Else
iRows = DirectCast(dg.DataSource, Collection).Count
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).GetType().GetProperty("Height")
Dim curHeight As Integer = pi.GetValue(arHeights(x), Nothing)
pi.SetValue(arHeights(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.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
GetHeightList()
End If

cm = source

'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String = Me.GetColumnValueAtRow([source],
rowNum).ToString()

Dim r As New RectangleF(bounds.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(rowNum).GetType().GetProperty("Height")
' get current height
Dim curHeight As Integer = pi.GetValue(arHeights(rowNum),
Nothing)

' adjust height

If h > curHeight Then
pi.SetValue(arHeights(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 HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

Public Property AutoAdjustHeight() 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_PositionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.PositionChanged
Static intOld As Integer = 0
'If intOld > cm.Position Then
If cm.Count > DirectCast(dg.DataSource, DataTable).Rows.Count Then
Debug.WriteLine("New Row")

For x As Integer = 0 To arHeights.Count - 1
Dim pi As PropertyInfo =
arHeights(x).GetType().GetProperty("Height")
Dim curHeight As Integer = pi.GetValue(arHeights(x),
Nothing)
pi.SetValue(arHeights(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*******@rislaa.no> wrote in message
news:K3******************@news4.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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.