DataGridSexBoxColumn类参考
该类继承自DataGridColumnStyle类。该类可以用于向BriskDataGrid添加一个SexBox列,以显示并设置性别数据。
属性
AllowUnknow——是否允许单元格接受并表示性别不详。
MaleValue——单元格的值为男性时的实际值。
FemaleValue——单元格的值为女性时的实际值。
UnknowValue——单元格的值为性别不详时的实际值。
MaleText——单元格为男性的时候在单元格里显示的文本。
FemaleText——单元格为女性的时候在单元格里显示的文本。
UnknowText——单元格为性别不详的时候在单元格里显示的文本。
Alignment——获取或设置列中文本的对齐方法。
HeaderText——获取或设置列标头文本。
MappingName——获取或设置用于将列样式映射到数据成员的名称。
NullText——获取或设置在列包含空引用时所显示的文本。
ReadOnly——获取或设置一个值,该值指示该列中的数据是否可以编辑。
Width——获取或设置列的宽度。
源码参考:
Public Class DataGridSexBoxColumn
Inherits DataGridColumnStyle
'是否正在被编辑
Protected _IsEditing As Boolean
'SexBox
Protected SexBox As New SexBox
'是否允许性别不详
Public Property AllowUnknow() As Boolean
Get
Return Me.SexBox.AllowUnknow
End Get
Set(ByVal Value As Boolean)
Me.SexBox.AllowUnknow = Value
End Set
End Property
'单元格值为男性时的实际值
Public Property MaleValue() As Object
Get
Return Me.SexBox.MaleValue
End Get
Set(ByVal Value As Object)
Me.SexBox.MaleValue = Value
End Set
End Property
'把单元格值设为女性时的实际值
Public Property FemaleValue() As Object
Get
Return Me.SexBox.FemaleValue
End Get
Set(ByVal Value As Object)
Me.SexBox.FemaleValue = Value
End Set
End Property
'把单元格值设为性别不详时的实际值
Public Property UnknowValue() As Object
Get
Return Me.SexBox.UnknowValue
End Get
Set(ByVal Value As Object)
Me.SexBox.UnknowValue = Value
End Set
End Property
'单元格值为男性时在单元格里显示的文本
Public Property MaleText() As String
Get
Return Me.SexBox.MaleText
End Get
Set(ByVal Value As String)
Me.SexBox.MaleText = Value
End Set
End Property
'单元格值为女性时在单元格里显示的文本
Public Property FemaleText() As String
Get
Return Me.SexBox.FemaleText
End Get
Set(ByVal Value As String)
Me.SexBox.FemaleText = Value
End Set
End Property
Protected _UnknowText As String = "不详"
'单元格值为性别不详时在单元格里显示的文本
Public Property UnknowText() As String
Get
Return Me._UnknowText
End Get
Set(ByVal Value As String)
Me._UnknowText = Value
End Set
End Property
Public Sub New()
Me.Initialize()
End Sub
Protected Sub Initialize()
Me.SexBox.Visible = False
Me.SexBox.UnknowValue = Convert.DBNull
End Sub
'只读策略
Protected Overridable Function IsReadOnly() As Boolean
Return Me.ReadOnly
End Function
'SexBox的SexValue已经发生改变
'(当SexBox.SexValue发生改变时执行此函数)
Protected Overridable Sub SexValueChanged( _
ByVal sender As Object, ByVal e As EventArgs)
'卸载DateTimePicker的ValueChanged事件
RemoveHandler SexBox.SexValueChanged, _
AddressOf SexValueChanged
'正在编辑宿主控件(这是唯一一次将isEditing设置为true)
Me._IsEditing = True
'通知DataGrid用户已开始编辑该列
MyBase.ColumnStartedEditing(SexBox)
End Sub
Protected Overrides Sub Abort(ByVal rowNum As Integer)
'用户没有正在编辑宿主控件
_IsEditing = False
'卸载句柄
RemoveHandler SexBox.SexValueChanged, _
AddressOf SexValueChanged
'重新绘制列并向控件发送一条绘制消息
Invalidate()
End Sub
Protected Overrides Function Commit( _
ByVal dataSource As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Boolean
'置零sexbox的区域
SexBox.Bounds = Rectangle.Empty
'卸载SexValueChanged句柄
RemoveHandler SexBox.SexValueChanged, _
AddressOf SexValueChanged
'如果isEditing不为真
If Not _IsEditing Then
Return True '返回true
End If
'isEditing等于false
_IsEditing = False
'得到SexBox的值
Dim value As Object = Me.SexBox.GetSexValue
'用来自指定 CurrencyManager 的值设置指定行中的值
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
'就从这里返回,不让单元格成为SexBox
Return
End If
'从单元格得到值
Dim value As Object
value = Me.GetColumnValueAtRow(source, rowNum)
'如果单元格是可见的值
If cellIsVisible Then
'内嵌控件在单元格之内,比单元格小2个像素,小一圈
SexBox.Bounds = New Rectangle _
(bounds.X + 2, bounds.Y + 2, bounds.Width - 4, _
bounds.Height - 4)
'设置内嵌控件的外观,主要是颜色,为了使外观统一
'得到DataGridTableStyle的各种颜色设置
Dim AlternatingBackColor As Color = Me.DataGridTableStyle.AlternatingBackColor
Dim BackColor As Color = Me.DataGridTableStyle.BackColor
Dim ForeColor As Color = Me.DataGridTableStyle.ForeColor
Dim SelectionForeColor As Color = Me.DataGridTableStyle.SelectionForeColor
Dim SelectionBackColor As Color = Me.DataGridTableStyle.SelectionBackColor
'根据以上颜色设置内嵌控件的颜色
If (rowNum Mod 2) <> 0 Then
Me.SexBox.MaleBackColor = AlternatingBackColor
Me.SexBox.FemaleBackColor = AlternatingBackColor
Else
Me.SexBox.MaleBackColor = BackColor
Me.SexBox.FemaleBackColor = BackColor
End If
Me.SexBox.MaleForeColor = ForeColor
Me.SexBox.FemaleForeColor = ForeColor
Me.SexBox.SelectionMaleBackColor = SelectionBackColor
Me.SexBox.SelectionFemaleBackColor = SelectionBackColor
Me.SexBox.SelectionMaleForeColor = SelectionForeColor
Me.SexBox.SelectionFemaleForeColor = SelectionForeColor
'把从单元格里得到的值给内嵌控件
Me.SexBox.SetSexValue(value)
'可见
Me.SexBox.Visible = True
'向DataGrid发送一条绘制消息,重新绘制指定区域
DataGridTableStyle.DataGrid.Invalidate(bounds)
'内嵌控件得到焦点
Me.SexBox.Focus()
'添加内嵌控件的事件句柄
AddHandler SexBox.SexValueChanged, _
AddressOf SexValueChanged
Else '否则
'把从单元格里得到的值给内嵌控件
Me.SexBox.SetSexValue(value)
'不可见
Me.SexBox.Visible = False
End If
End Sub
Protected Overrides Function GetMinimumHeight() As Integer
End Function
Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
End Function
Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size
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 strSex As String
Dim value As Object = Me.GetColumnValueAtRow(source, rowNum)
Try
If value.Equals(Me.MaleValue) Then
strSex = Me.MaleText
ElseIf value.Equals(Me.FemaleValue) Then
strSex = Me.FemaleText
ElseIf value.Equals(Me.UnknowValue) Then
If Me.AllowUnknow Then
strSex = Me.UnknowText
Else
Throw New Exception(Me.GetType.ToString & ".AllowUnknow = " & Me.AllowUnknow.ToString & "所以SexValue属性不能接受UnknowValue")
End If
Else
Throw New Exception(Me.GetType.ToString & ".SexValue属性无法接受" & value.ToString & _
"。该属性只能接受MaleValue、FemaleValue、UnknowValue的值。")
End If
Catch ex As Exception
Throw ex
End Try
'判断是否从右向左
If alignToRight Then
Me._TextFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
'画出字符串
rect.Offset(0, 2)
rect.Height -= 2
g.DrawString(strSex, _
Me.DataGridTableStyle.DataGrid.Font, foreBrush, _
RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom), Me._TextFormat)
End Sub
Protected Overrides Sub SetDataGridInColumn(ByVal value As System.Windows.Forms.DataGrid)
'调用父类的此函数
MyBase.SetDataGridInColumn(value)
'如果picker的父控件不是nothing
If Not (SexBox.Parent Is Nothing) Then
'从picker的父控件里卸载picker
SexBox.Parent.Controls.Remove(SexBox)
End If
'如果value(DataGrid)不是noting
If Not (value Is Nothing) Then
'把Picker添加到value(DataGrid)的Controls里面
value.Controls.Add(SexBox)
End If
End Sub
'从单元格里得到值
Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object
Dim obj As Object
obj = MyBase.GetColumnValueAtRow(source, rowNum)
Return obj
End Function
'把值送回单元格里
Protected Overrides Sub SetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal value As Object)
MyBase.SetColumnValueAtRow(source, rowNum, value)
End Sub
Protected Overrides Sub ConcedeFocus()
MyBase.ConcedeFocus()
Me._IsEditing = False
End Sub
End Class