DataGridTextColumn类参考
该类继承自DataGridColumnStyle类。该类用于向BriskDataGrid添加一个Text列。
属性
Alignment——获取或设置列中文本的对齐方法。
HeaderText——获取或设置列标头文本。
MappingName——获取或设置用于将列样式映射到数据成员的名称。
NullText——获取或设置在列包含空引用时所显示的文本。
ReadOnly——获取或设置一个值,该值指示该列中的数据是否可以编辑。
Width——获取或设置列的宽度。
源码参考:
Public Class DataGridTextColumn
Inherits DataGridColumnStyle
Public Sub New()
Me._isEditing = False
Me.TextBox.Visible = False
End Sub
Protected TextBox As New TextBox
'标志正在被编辑
Protected _isEditing As Boolean
'只读策略
Protected Overridable Function IsReadOnly() As Boolean
Return Me.ReadOnly
End Function
Protected Overridable Sub TextChanged( _
ByVal sender As Object, _
ByVal e As EventArgs)
RemoveHandler TextBox.TextChanged, _
AddressOf TextChanged
Me._isEditing = True
MyBase.ColumnStartedEditing(Me.TextBox)
End Sub
Protected Overrides Sub Abort(ByVal rowNum As Integer)
Me.TextBox.Visible = False
Me._isEditing = False
RemoveHandler TextBox.TextChanged, _
AddressOf TextChanged
Invalidate()
End Sub
Protected Overrides Function Commit( _
ByVal dataSource As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Boolean
'置零内嵌控件的显示区域
TextBox.Bounds = Rectangle.Empty
'卸载内嵌控件的ValueChanged事件
RemoveHandler TextBox.TextChanged, _
AddressOf TextChanged
'如果isEditing不为真
If Not _isEditing Then
Return True '返回true
End If
'isEditing等于false
_isEditing = False
'得到内嵌控件的值
Dim value As String = TextBox.Text
'把值送入单元格
Me.SetColumnValueAtRow(dataSource, rowNum, value)
'重新绘制列并向控件发送一条绘制消息
Invalidate()
'返回true
Return True
End Function
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)
'如果列是只读的
If Me.IsReadOnly Then
'就从这里返回
Return
End If
'得到单元格里的值
Dim value As String = Me.GetColumnValueAtRow(source, rowNum)
'把从单元格里得到的值给内嵌控件
Me.TextBox.Text = value
'如果单元格是可见的值
If cellIsVisible Then
'添加事件句柄添加内嵌控件的值改变事件
AddHandler TextBox.TextChanged, _
AddressOf TextChanged
'设置内嵌控件的外观,使外观颜色统一
'Me.TextBox.BackColor = Me.DataGridTableStyle.SelectionBackColor()
'Me.TextBox.ForeColor = Me.DataGridTableStyle.SelectionForeColor()
'可见
Me.TextBox.SetBounds( _
bounds.X, bounds.Y, _
bounds.Width, bounds.Height)
Me.TextBox.TextAlign = Me.Alignment
Me.TextBox.Visible = True
DataGridTableStyle.DataGrid.Invalidate(bounds)
Else '否则
'不可见
Me.TextBox.Visible = False
End If
'内嵌控件得到焦点
Me.TextBox.Focus()
End Sub
Protected Overrides Function GetMinimumHeight() As Integer
Return Me.TextBox.PreferredHeight + 4
End Function
Protected Overrides Function GetPreferredHeight( _
ByVal g As System.Drawing.Graphics, _
ByVal value As Object) As Integer
Return Me.TextBox.PreferredHeight + 4
End Function
Protected Overrides Function GetPreferredSize( _
ByVal g As System.Drawing.Graphics, _
ByVal value As Object) As System.Drawing.Size
Return New Size(100, Me.TextBox.PreferredHeight + 4)
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)
Me.Paint(g, bounds, source, rowNum, False)
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 alignToRight As Boolean)
Me.Paint(g, bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight)
End Sub
Public Overrides Property Alignment() As HorizontalAlignment
Get
Return MyBase.Alignment
End Get
Set(ByVal Value As HorizontalAlignment)
MyBase.Alignment = Value
If Value = HorizontalAlignment.Left Then
Me._TextFormat.Alignment = StringAlignment.Near
ElseIf Value = HorizontalAlignment.Center Then
Me._TextFormat.Alignment = StringAlignment.Center
ElseIf Value = HorizontalAlignment.Right Then
Me._TextFormat.Alignment = StringAlignment.Far
End If
End Set
End Property
'显示在单元格里的文本的格式
Protected _TextFormat As New StringFormat
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)
'在这里把列画出来
Dim rect As Rectangle = bounds
'把单元格的背景画出来
g.FillRectangle(backBrush, rect)
'得到单元格里的值并且处理成指定格式的字符串
Dim str As String
Dim obj As Object = Me.GetColumnValueAtRow(source, rowNum)
If obj = Nothing Then
str = Me.NullText
Else
str = obj.ToString
End If
'判断是否从右向左
If alignToRight Then
Me._TextFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
'调整一下范围
rect.Offset(0, 2)
rect.Height -= 2
'画出字符串
g.DrawString(str, _
Me.DataGridTableStyle.DataGrid.Font, foreBrush, _
RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom), Me._TextFormat)
End Sub
'从单元格里得到值,还要决定对dbnull的处理策略
Protected Overrides Function GetColumnValueAtRow( _
ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Object
Dim obj As Object
'调用父类的同名函数,得到单元格的值,并且送入obj
obj = MyBase.GetColumnValueAtRow(source, rowNum)
'如果该值完全等于dbnull
If obj.Equals(Convert.DBNull) Then
'就让obj等于nothing
obj = ""
End If
Return obj
End Function
Protected Overrides Sub SetColumnValueAtRow( _
ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer, _
ByVal value As Object)
Dim obj As Object
'如果从内嵌控件传递过来的值等于nothing
If value Is Nothing Then
'obj赋值为dbnull
obj = Convert.DBNull
Else '否则
'obj赋值为value
obj = value
End If
'调用父类的同名函数,把obj传递给单元格
MyBase.SetColumnValueAtRow(source, rowNum, obj)
End Sub
Protected Overrides Sub SetDataGridInColumn( _
ByVal value As System.Windows.Forms.DataGrid)
'调用父类的此函数
MyBase.SetDataGridInColumn(value)
'如果内嵌控件有父控件
If Not (Me.TextBox.Parent Is Nothing) Then
'从父控件里卸载内嵌控件
Me.TextBox.Parent.Controls.Remove(Me.TextBox)
End If
'如果value(DataGrid)不是noting
If Not (value Is Nothing) Then
'把内嵌控件添加到value(DataGrid)的Controls里面
value.Controls.Add(Me.TextBox)
End If
End Sub
Protected Overrides Sub ConcedeFocus()
MyBase.ConcedeFocus()
Me._isEditing = False
End Sub
End Class
'Public Class DataGridTextBox
'Inherits System.Windows.Forms.TextBox
'Protected Overrides Function ProcessKeyMessage(ByRef m As Message) As Boolean
'为textbox保留所有的键
' Return ProcessKeyEventArgs(m)
'End Function
'End Class