473,765 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to change row height in a DataGrid

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
2 3398
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

Nov 20 '05 #2
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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1930
by: Serdar Kalaycý | last post by:
I have a DataGrid in my WebForm. I set its Height propert to 1px because if I give it a big number and there's less records then expected, it extends the rows giving a silly layout. So it gets longer as the records appear. But I'd like to add some textboxes to add a new record to the database (as I see, there's no method of datagrid to add new records) below the DataGrid and I'd like to set their Location dynamically exatly where the...
5
1876
by: Kat | last post by:
Hi, I'm trying to set up an asp.net page using flow layout so I'm putting all my controls into a table grid, etc. I use several radiobuttonlists and datagrids that are generated from datasource. The radiobuttonlists are fine, they size depending on how many item are called into it. However, the datagrids, prior to filling with data contain FIVE "Databound" items and the grid does not shrink if it is filled with only 2 for example....
3
2157
by: Mau | last post by:
I have a question: I have a datagrid with paging (4 elements for page), header and footer I set a fixed height: 600px So the situation is (in a page with 5 elements) 6 rows (header + 4 elements + footer) every with fixed height 100px, and this is ok but in a page with 1 element (for example) I have 3 rows (header + 1 element + footer) every row is 200px height
0
1549
by: csgraham74 | last post by:
Hi there, i have creted a datagrid control to display images using the following code. <asp:DataGrid id="dg_Properties" runat="server" HorizontalAlign="Center" Width="450px" AutoGenerateColumns="False" ShowHeader="False"> <Columns><asp:BoundColumn Visible="False" DataField="Property_ID" HeaderText="Property_ID"></asp:BoundColumn><asp:TemplateColumn><ItemTemplate><table
2
2840
by: Elijsh | last post by:
I'm new to datagrid and VB.Net. Is there any way to adjust the height of datagrid textbox row according to the height of text programmatically ? I mean I have multiple data with varied length, some have one rows, some have three rows and maybe seven. How can I set the height programmaticalky? Your advice is greatly appreciated!
5
4741
by: Dennis | last post by:
I have a class that inherits from DataGrid. I can set the rowheights in a DataGrid by tappig into the "get_Datagridrows" method. However, this does not work for classes that inherit from DataGrid. Anyone know how to do this on derived classes? I find it apalling that Microsoft provided a DataGrid that is so inflexible! -- Dennis in Houston
3
22037
by: Sharon | last post by:
I have a DataGrid that contains a few rows and column header. I wish to change the DataGrid height to be the exact height of the total height of the rows and column header, so that no gray area will be shown below the last row. I tried: int totalRowsHeight = dataTable.Rows.Count * (dataGridTableStyle.PreferredRowHeight + 1); Size gridClientSize = dataGrid.ClientSize;
7
3241
by: Localbar | last post by:
hi all, my problem is...i have 2 textbox for user input no. of row and no.of column. After user input, the datagrid will show what user input....for example: user input 5 rows and 6 column. After that...the datagrid will show.I would like to control the height for datagrid after generate... but i only can control the width....any one have idea about this. Thanks
3
1496
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
I have data that spans over several pages in a datagrid. On the last page, the row heights seem to justify vertically, so if there are say 14 records and the paging is ten, each row on the last page will take up a quarter of the grid height. Is there any way so that the row height will always be the same regardless of the height of the grid or the number of rows being displayed? --
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.