Multiline DataGrid

G

Guest

I'm trying to upgrade the DataGrid so that I can display multiple lines of text in one row. My method is to create a new class derived from DataGridTextBoxColumn. As suggested in http://www.dotnet247.com/247reference/msgs/20/100505.aspx I have overridden the 2 GetHeight methods. However, I can't override PaintText because it is not virtual or override.

As a result, when editting the text it works great. When viewing, the rows are the correct height but the text is only one line

Is there an easy way to do this, or do I have to rewrite the DataGridTextBoxColumn class myself?
 
K

Ken Tucker [MVP]

Hi,

Here is a multiline datagrid column I have been working on. Hope it
helps.

Imports System.Reflection

Public Class MultiLineColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment

Private mDrawTxt As New StringFormat

Private mbAdjustHeight As Boolean = True

Dim dg As DataGrid

Private arHeights As ArrayList

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

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)

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.Multiline = mbAdjustHeight

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

'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)

End If

Catch

' row not in index recreate list

GetHeightList()

Try

' one last try

Dim pi As PropertyInfo = arHeights(rowNum).GetType().GetProperty("Height")

Dim curHeight As Integer = pi.GetValue(arHeights(rowNum), Nothing)

If h > curHeight Then

pi.SetValue(arHeights(rowNum), h, Nothing)

End If

Catch

' something wrong leave default height

End Try

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

End Class



Ken

---------------------
 
G

Guest

That's great. It has a slight bug when you finish typing in a row, displaying a (null) in the middle of the first row, but that graphical artifact goes away as soon as you click somewhere. Also the box doesn't expand until you're done typing. But minor problems in general! This is great. If I make any improvements I will post them back here.
 
J

Jan Tielens

Emmett

You may want to check out the (free) ExtendedDataGrid control:
http://dotnet.leadit.be/extendeddatagrid

It has a multiline textbox column.

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


Emmett said:
That's great. It has a slight bug when you finish typing in a row,
displaying a (null) in the middle of the first row, but that graphical
artifact goes away as soon as you click somewhere. Also the box doesn't
expand until you're done typing. But minor problems in general! This is
great. If I make any improvements I will post them back here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top