PC Review


Reply
Thread Tools Rate Thread

How to change row height in a DataGrid

 
 
Tor Inge Rislaa
Guest
Posts: n/a
 
      20th May 2004
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      20th May 2004
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("DataGridRelationshipRow") =
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" <(E-Mail Removed)> wrote in message
news:K34rc.2682$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Row height in datagrid =?Utf-8?B?UGhpbGlwIEdlcm1hbm9z?= Microsoft ADO .NET 1 18th Jun 2005 12:01 AM
Row height in a datagrid Geoff Jones Microsoft VB .NET 2 9th Jan 2005 12:32 PM
change DataGrid row height programmatically =?Utf-8?B?U0JVSk9MRA==?= Microsoft Dot NET Compact Framework 1 10th Jun 2004 11:19 PM
change DataGrid row height programmatically =?Utf-8?B?U0JVSk9MRA==?= Microsoft Dot NET Compact Framework 0 10th Jun 2004 10:12 PM
taskbar height doubled - can't change back to normal height Lyn Windows XP General 2 20th Oct 2003 11:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:53 AM.