DataGridDateTimePickerColumn类参考

 

DataGridDateTimePickerColumn类参考


该类继承自DataGridColumnStyle类。该类用于在BriskDataGrid中处理日期时间。


属性


Format——获取或设置在单元格中显示日期时间的格式。

ShowUpDown——获取或设置是否在DateTimePicker里显示上下按钮。

Alignment——获取或设置列中文本的对齐方法。

HeaderText——获取或设置列标头文本。

MappingName——获取或设置用于将列样式映射到数据成员的名称。

NullText——获取或设置在列包含空引用时所显示的文本。

ReadOnly——获取或设置一个值,该值指示该列中的数据是否可以编辑。

Width——获取或设置列的宽度。

源码参考:


Public Class DataGridDateTimePickerColumn
    Inherits DataGridColumnStyle

    Protected DateTimePicker As DataGridDateTimePicker
    '正在被编辑标志
    Protected _isEditing As Boolean

    '内嵌控件值已经发生改变
    '(当DateTimePicker.Value发生改变时执行此函数)
    Protected Overridable Sub DateTimePickerValueChanged( _
        ByVal sender As Object, ByVal e As EventArgs)

        '卸载内嵌控件的ValueChanged事件
        RemoveHandler DateTimePicker.ValueChanged, _
            AddressOf DateTimePickerValueChanged
        '正在编辑宿主控件(这是唯一一次将isEditing设置为true)
        Me._isEditing = True
        '通知DataGrid用户已开始编辑该列
        MyBase.ColumnStartedEditing(DateTimePicker)
    End Sub

    '启动一个请求来中断编辑过程
    Protected Overrides Sub Abort(ByVal rowNum As Integer)
        '用户没有正在编辑宿主控件
        _isEditing = False
        '卸载内嵌控件的ValueChanged句柄
        RemoveHandler DateTimePicker.ValueChanged, _
            AddressOf DateTimePickerValueChanged
        '重新绘制列并向控件发送一条绘制消息
        Invalidate()
    End Sub

    '提交
    Protected Overrides Function Commit( _
        ByVal dataSource As System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer) As Boolean

        '置零内嵌控件的显示区域
        DateTimePicker.Bounds = Rectangle.Empty

        '卸载内嵌控件的ValueChanged句柄
        RemoveHandler DateTimePicker.ValueChanged, _
            AddressOf DateTimePickerValueChanged

        '如果isEditing不为真
        If Not _isEditing Then
            Return True '返回true
        End If
        'isEditing等于false
        _isEditing = False

        '得到内嵌控件的值
        Dim value As Object = DateTimePicker.Value
        '用来自指定 CurrencyManager 的值设置指定行中的值
        SetColumnValueAtRow(dataSource, rowNum, value)

        '重新绘制列并向控件发送一条绘制消息
        Invalidate()
        '返回true
        Return True
    End Function

    '参数
    'source为DataGridColumnStyle 的 CurrencyManager
    'rowNum是此列中所编辑的行的行号
    'bounds是一个Rectangle,控件将被放置在其中
    'readOnly指示该列是否为只读的列的值。如果该值是只读,则为 true;否则为 false
    'instantText控件中将显示的文本
    'cellIsVisible指示该单元格是否可见的值。如果该单元格可见,则为 true;否则为 false
    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
            '就从这里返回,不让单元格成为picker
            Return
        End If

        '从单元格得到值     
        Dim value As DateTime = _
                CType(GetColumnValueAtRow([source], rowNum), DateTime)
        If value < DateTimePicker.MinDate Then
            value = DateTimePicker.MinDate
        Else
            If value > DateTimePicker.MaxDate Then
                value = DateTimePicker.MaxDate
            End If
        End If
        '把从单元格里得到的值给内嵌控件
        DateTimePicker.Value = value
        '如果单元格是可见的值
        If cellIsVisible Then
            '内嵌控件在单元格之内,小高和宽都小2像素
            DateTimePicker.Bounds = New Rectangle _
            (bounds.X + 2, bounds.Y + 2, bounds.Width - 4, _
            bounds.Height - 4)

            '设置内嵌控件的外观
            Me.DateTimePicker.CalendarForeColor = Me.DataGridTableStyle.ForeColor
            Me.DateTimePicker.CalendarMonthBackground = Me.DataGridTableStyle.BackColor
            Me.DateTimePicker.CalendarTitleBackColor = Me.DataGridTableStyle.HeaderBackColor
            Me.DateTimePicker.CalendarTitleForeColor = Me.DataGridTableStyle.HeaderForeColor
            Me.DateTimePicker.CalendarTrailingForeColor = Me.DataGridTableStyle.ForeColor
            '可见
            DateTimePicker.Visible = True
            '向DataGrid发送一条绘制消息,重新绘制指定区域
            DataGridTableStyle.DataGrid.Invalidate(bounds)
            'picker得到焦点
            DateTimePicker.Focus()
            '添加事件句柄picker的ValueChanged
            AddHandler DateTimePicker.ValueChanged, _
                AddressOf DateTimePickerValueChanged
        Else '否则          
            '不可见
            DateTimePicker.Visible = False
        End If
    End Sub

    Protected Overrides Function GetMinimumHeight() As Integer
        Return DateTimePicker.PreferredHeight + 4
    End Function

    Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
        Return DateTimePicker.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, DateTimePicker.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)
        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)

        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 Graphics, _
       ByVal bounds As Rectangle, _
       ByVal [source] As CurrencyManager, _
       ByVal rowNum As Integer, _
       ByVal backBrush As Brush, _
       ByVal foreBrush As Brush, _
       ByVal alignToRight As Boolean)

        Dim rect As Rectangle = bounds
        '填充单元格背景
        g.FillRectangle(backBrush, rect)

        '从单元格得到值
        Dim obj1 As Object
        obj1 = GetColumnValueAtRow([source], rowNum)
        Dim [date] As Date
        [date] = CType(obj1, Date)
        '把日期转换成字符串
        Dim str As String
        If [date] = Nothing Then
            str = Me.NullText
        Else
            str = [date].ToString(Me._Format)
        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

    '把内嵌控件加入DataGrid
    Protected Overrides Sub SetDataGridInColumn(ByVal value As System.Windows.Forms.DataGrid)

        '调用父类的此函数
        MyBase.SetDataGridInColumn(value)
        '如果picker的父控件不是nothing
        If Not (DateTimePicker.Parent Is Nothing) Then
            '从picker的父控件里卸载picker
            DateTimePicker.Parent.Controls.Remove(DateTimePicker)
        End If
        '如果value(DataGrid)不是noting
        If Not (value Is Nothing) Then
            '把Picker添加到value(DataGrid)的Controls里面
            value.Controls.Add(DateTimePicker)
        End If
    End Sub

    '只读策略
    Protected Overridable Function IsReadOnly() As Boolean
        Return Me.ReadOnly
    End Function

    Protected Overridable Sub Initialize()
        Me.DateTimePicker = New DataGridDateTimePicker
        '初始时picker不可见
        Me.DateTimePicker.Visible = False
    End Sub

    Public Sub New()

        Me.Initialize()
    End Sub

    '显示日期时间的格式
    Protected _Format As String = "yyyy年MM月dd日 HH时mm分ss秒"
    Public Overridable Property Format() As String
        Get
            Return Me._Format
        End Get
        Set(ByVal Value As String)
            Me._Format = Value
            'picker的格式与cell里的格式保持一致
            Me.DateTimePicker.Format = DateTimePickerFormat.Custom
            Me.DateTimePicker.CustomFormat = Me._Format
        End Set
    End Property

    '是否在picker里面显示上下按钮
    Public Overridable Property ShowUpDown() As Boolean
        Get
            Return Me.DateTimePicker.ShowUpDown()
        End Get
        Set(ByVal Value As Boolean)
            Me.DateTimePicker.ShowUpDown = Value
        End Set
    End Property

    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)

        If obj.Equals(Convert.DBNull) Then
            obj = Nothing
        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
        If value = Nothing Then
            obj = Convert.DBNull
        Else
            obj = value
        End If

        MyBase.SetColumnValueAtRow(source, rowNum, obj)
    End Sub

    Protected Overrides Sub ConcedeFocus()
        MyBase.ConcedeFocus()
        Me._isEditing = False
    End Sub
End Class

Public Class DataGridDateTimePicker
    Inherits DateTimePicker

    Protected Overrides Function ProcessKeyMessage(ByRef m As Message) As Boolean
        '为DateTimePicker保留所有的键
        Return ProcessKeyEventArgs(m)
    End Function
End Class







你可能感兴趣的:(DataGridDateTimePickerColumn类参考)