Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Datagrid MultiLine working!

60 views
Skip to first unread message

Joel Mendez

unread,
May 19, 2003, 5:30:55 PM5/19/03
to
Hi, for those of you trying to make a DataGrid textbox column to be
Multiline and WordWrap:

1. Change the PreferredRowHeight property of the TableStyle (50 shows
4 lines of text)

2. Set up the ColumnsStyle of the columns you want to be wordwraped
to the following type: MultiLineTextBox.


Here is the code for the ColumnStyle:

Public Class MultiLineTextBox
Inherits System.Windows.Forms.DataGridTextBoxColumn

Private _source As CurrencyManager
Private _rowNumber As Integer
Private _isEditing As Boolean

Public Sub New()
MyBase.New()

_source = Nothing
_isEditing = False

Me.TextBox.ScrollBars = ScrollBars.Vertical

AddHandler MyBase.TextBox.TextChanged, AddressOf
TextBoxStartEditing
AddHandler MyBase.TextBox.Leave, AddressOf LeaveTextBox
End Sub

Private Sub TextBoxStartEditing(ByVal sender As Object, ByVal e As
EventArgs)
_isEditing = True

MyBase.ColumnStartedEditing(CType(sender, Control))
End Sub

Private Sub LeaveTextBox(ByVal sender As Object, ByVal e As
EventArgs)
If _isEditing Then
SetColumnValueAtRow(_source, _rowNumber,
MyBase.TextBox.Text)
_isEditing = False
End If

Invalidate()
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle,
ByVal readOnly1 As Boolean, ByVal instantText As String, ByVal
cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText,
cellIsVisible)

_rowNumber = rowNum
_source = source

End Sub

Protected Overloads Overrides Function Commit(ByVal dataSource As
CurrencyManager, ByVal rowNum As Integer) As Boolean
If _isEditing Then
_isEditing = False
SetColumnValueAtRow(dataSource, rowNum,
MyBase.TextBox.Text)
End If

Me.TextBox.Visible = False
Return True
End Function

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)
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight)

g.FillRectangle(backBrush, bounds)
Dim vText As String = CType(MyBase.GetColumnValueAtRow(source,
rowNum), String)
g.DrawString(vText, Me.TextBox.Font, foreBrush, New
RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height))
End Sub

End Class

Please let me know any issue or recommendation...

0 new messages