给DATAGRIDVIEW添加自定义DATETIME列

在微软网站上找到的添加自定义DATETIME列,给大家共享以下
None.gif Imports  System
None.gif
Imports  System.Windows.Forms
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Class CalendarColumn Class CalendarColumn
InBlock.gif    
Inherits DataGridViewColumn
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New()
InBlock.gif        
MyBase.New(New CalendarCell())
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Overrides Property CellTemplate()Property CellTemplate() As DataGridViewCell
InBlock.gif        
Get
InBlock.gif            
Return MyBase.CellTemplate
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As DataGridViewCell)
InBlock.gif
InBlock.gif            
' Ensure that the cell used for the template is a CalendarCell.
InBlock.gif
            If Not (value Is NothingAndAlso _
InBlock.gif                
Not value.GetType().IsAssignableFrom(GetType(CalendarCell)) _
InBlock.gif                
Then
InBlock.gif                
Throw New InvalidCastException("Must be a CalendarCell")
InBlock.gif            
End If
InBlock.gif            
MyBase.CellTemplate = value
InBlock.gif
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Class CalendarCell Class CalendarCell
InBlock.gif    
Inherits DataGridViewTextBoxCell
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New()
InBlock.gif        
' Use the short date format.
InBlock.gif
        Me.Style.Format = "d"
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Overrides Sub InitializeEditingControl()Sub InitializeEditingControl(ByVal rowIndex As Integer, _
InBlock.gif        
ByVal initialFormattedValue As Object, _
InBlock.gif        
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
InBlock.gif
InBlock.gif        
' Set the value of the editing control to the current cell value.
InBlock.gif
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
InBlock.gif            dataGridViewCellStyle)
InBlock.gif
InBlock.gif        
Dim ctl As CalendarEditingControl = _
InBlock.gif            
CType(DataGridView.EditingControl, CalendarEditingControl)
InBlock.gif        ctl.Value 
= CType(Me.Value, DateTime)
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Overrides ReadOnly Property EditType()Property EditType() As Type
InBlock.gif        
Get
InBlock.gif            
' Return the type of the editing contol that CalendarCell uses.
InBlock.gif
            Return GetType(CalendarEditingControl)
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Overrides ReadOnly Property ValueType()Property ValueType() As Type
InBlock.gif        
Get
InBlock.gif            
' Return the type of the value that CalendarCell contains.
InBlock.gif
            Return GetType(DateTime)
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Overrides ReadOnly Property DefaultNewRowValue()Property DefaultNewRowValue() As Object
InBlock.gif        
Get
InBlock.gif            
' Use the current date and time as the default value.
InBlock.gif
            Return DateTime.Now
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Class CalendarEditingControl Class CalendarEditingControl
InBlock.gif    
Inherits DateTimePicker
InBlock.gif    
Implements IDataGridViewEditingControl
InBlock.gif
InBlock.gif    
Private dataGridViewControl As DataGridView
InBlock.gif    
Private valueIsChanged As Boolean = False
InBlock.gif    
Private rowIndexNum As Integer
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New()
InBlock.gif        
Me.Format = DateTimePickerFormat.Short
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property EditingControlFormattedValue()Property EditingControlFormattedValue() As Object _
InBlock.gif        
Implements IDataGridViewEditingControl.EditingControlFormattedValue
InBlock.gif
InBlock.gif        
Get
InBlock.gif            
Return Me.Value.ToShortDateString()
InBlock.gif        
End Get
InBlock.gif
InBlock.gif        
Set(ByVal value As Object)
InBlock.gif            
If TypeOf value Is [StringThen
InBlock.gif                
Me.Value = DateTime.Parse(CStr(value))
InBlock.gif            
End If
InBlock.gif        
End Set
InBlock.gif
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function GetEditingControlFormattedValue()Function GetEditingControlFormattedValue(ByVal context _
InBlock.gif        
As DataGridViewDataErrorContexts) As Object _
InBlock.gif        
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
InBlock.gif
InBlock.gif        
Return Me.Value.ToShortDateString()
InBlock.gif
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub ApplyCellStyleToEditingControl()Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
InBlock.gif        DataGridViewCellStyle) _
InBlock.gif        
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
InBlock.gif
InBlock.gif        
Me.Font = dataGridViewCellStyle.Font
InBlock.gif        
Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
InBlock.gif        
Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property EditingControlRowIndex()Property EditingControlRowIndex() As Integer _
InBlock.gif        
Implements IDataGridViewEditingControl.EditingControlRowIndex
InBlock.gif
InBlock.gif        
Get
InBlock.gif            
Return rowIndexNum
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As Integer)
InBlock.gif            rowIndexNum 
= value
InBlock.gif        
End Set
InBlock.gif
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function EditingControlWantsInputKey()Function EditingControlWantsInputKey(ByVal key As Keys, _
InBlock.gif        
ByVal dataGridViewWantsInputKey As BooleanAs Boolean _
InBlock.gif        
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
InBlock.gif
InBlock.gif        
' Let the DateTimePicker handle the keys listed.
InBlock.gif
        Select Case key And Keys.KeyCode
InBlock.gif            
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
InBlock.gif                Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
InBlock.gif
InBlock.gif                
Return True
InBlock.gif
InBlock.gif            
Case Else
InBlock.gif                
Return False
InBlock.gif        
End Select
InBlock.gif
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub PrepareEditingControlForEdit()Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
InBlock.gif        
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
InBlock.gif
InBlock.gif        
' No preparation needs to be done.
InBlock.gif

ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property RepositionEditingControlOnValueChange()Property RepositionEditingControlOnValueChange() _
InBlock.gif        
As Boolean Implements _
InBlock.gif        IDataGridViewEditingControl.RepositionEditingControlOnValueChange
InBlock.gif
InBlock.gif        
Get
InBlock.gif            
Return False
InBlock.gif        
End Get
InBlock.gif
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property EditingControlDataGridView()Property EditingControlDataGridView() As DataGridView _
InBlock.gif        
Implements IDataGridViewEditingControl.EditingControlDataGridView
InBlock.gif
InBlock.gif        
Get
InBlock.gif            
Return dataGridViewControl
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As DataGridView)
InBlock.gif            dataGridViewControl 
= value
InBlock.gif        
End Set
InBlock.gif
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property EditingControlValueChanged()Property EditingControlValueChanged() As Boolean _
InBlock.gif        
Implements IDataGridViewEditingControl.EditingControlValueChanged
InBlock.gif
InBlock.gif        
Get
InBlock.gif            
Return valueIsChanged
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As Boolean)
InBlock.gif            valueIsChanged 
= value
InBlock.gif        
End Set
InBlock.gif
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property EditingControlCursor()Property EditingControlCursor() As Cursor _
InBlock.gif        
Implements IDataGridViewEditingControl.EditingPanelCursor
InBlock.gif
InBlock.gif        
Get
InBlock.gif            
Return MyBase.Cursor
InBlock.gif        
End Get
InBlock.gif
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overrides Sub OnValueChanged()Sub OnValueChanged(ByVal eventargs As EventArgs)
InBlock.gif
InBlock.gif        
' Notify the DataGridView that the contents of the cell have changed.
InBlock.gif
        valueIsChanged = True
InBlock.gif        
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
InBlock.gif        
MyBase.OnValueChanged(eventargs)
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif
None.gif
链接如下 http://msdn2.microsoft.com/library/7tas5c80.aspx,里面还有C#版本。

转载于:https://www.cnblogs.com/luobos/archive/2005/11/11/273804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值