承载 DateTimePicker 控件的 DataGridColumnStyle(C#)

DataGridTimePickerColumn类代码
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Drawing;
None.gif
None.gif
public   class  DataGridTimePickerColumn : DataGridColumnStyle 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private DateTimePicker myDateTimePicker = new DateTimePicker();
InBlock.gif        
private bool isEditing;
InBlock.gif
InBlock.gif        
public DataGridTimePickerColumn() : base() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            myDateTimePicker.Visible 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Abort(int rowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            isEditing 
= false;
InBlock.gif            myDateTimePicker.ValueChanged 
-= new EventHandler(TimePickerValueChanged);
InBlock.gif            Invalidate();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override bool Commit(CurrencyManager dataSource, int rowNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            myDateTimePicker.Bounds 
= Rectangle.Empty;
InBlock.gif         
InBlock.gif            myDateTimePicker.ValueChanged 
-= new EventHandler(TimePickerValueChanged);
InBlock.gif
InBlock.gif            
if (!isEditing)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            isEditing 
= false;
InBlock.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DateTime value 
= myDateTimePicker.Value;
InBlock.gif                SetColumnValueAtRow(dataSource, rowNum, value);
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch (Exception) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Abort(rowNum);
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Invalidate();
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Edit(
InBlock.gif            CurrencyManager source, 
InBlock.gif            
int rowNum,
InBlock.gif            Rectangle bounds, 
InBlock.gif            
bool readOnly,
InBlock.gif            
string instantText, 
InBlock.gif            
bool cellIsVisible) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DateTime value 
= (DateTime)GetColumnValueAtRow(source, rowNum);
InBlock.gif            
if (cellIsVisible) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myDateTimePicker.Bounds 
= new Rectangle(bounds.X + 2, bounds.Y + 2
InBlock.gif                                                bounds.Width 
- 4, bounds.Height - 4);
InBlock.gif                myDateTimePicker.Value 
= value;
InBlock.gif                myDateTimePicker.Visible 
= true;
InBlock.gif                myDateTimePicker.ValueChanged 
+= new EventHandler(TimePickerValueChanged);
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myDateTimePicker.Value 
= value;
InBlock.gif                myDateTimePicker.Visible 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (myDateTimePicker.Visible)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataGridTableStyle.DataGrid.Invalidate(bounds);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override Size GetPreferredSize(Graphics g, object value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Size(100, myDateTimePicker.PreferredHeight + 4);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override int GetMinimumHeight() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return myDateTimePicker.PreferredHeight + 4;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override int GetPreferredHeight(Graphics g, object value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return myDateTimePicker.PreferredHeight + 4;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Paint(g, bounds, source, rowNum, 
false);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void Paint(
InBlock.gif            Graphics g, 
InBlock.gif            Rectangle bounds,
InBlock.gif            CurrencyManager source, 
InBlock.gif            
int rowNum,
InBlock.gif            
bool alignToRight) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Paint(
InBlock.gif                g,bounds, 
InBlock.gif                source, 
InBlock.gif                rowNum, 
InBlock.gif                Brushes.Red, 
InBlock.gif                Brushes.Blue, 
InBlock.gif                alignToRight);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void Paint(
InBlock.gif            Graphics g, 
InBlock.gif            Rectangle bounds,
InBlock.gif            CurrencyManager source, 
InBlock.gif            
int rowNum,
InBlock.gif            Brush backBrush, 
InBlock.gif            Brush foreBrush,
InBlock.gif            
bool alignToRight) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DateTime date 
= (DateTime)GetColumnValueAtRow(source, rowNum);
InBlock.gif            Rectangle rect 
= bounds;
InBlock.gif            g.FillRectangle(backBrush,rect);
InBlock.gif            rect.Offset(
02);
InBlock.gif            rect.Height 
-= 2;
InBlock.gif            g.DrawString(date.ToString(
"d"), this.DataGridTableStyle.DataGrid.Font, foreBrush, rect);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void SetDataGridInColumn(DataGrid value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.SetDataGridInColumn(value);
InBlock.gif            
if (myDateTimePicker.Parent != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myDateTimePicker.Parent.Controls.Remove(myDateTimePicker);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (value != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                value.Controls.Add(myDateTimePicker);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void TimePickerValueChanged(object sender, EventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.isEditing = true;
InBlock.gif            
base.ColumnStartedEditing(myDateTimePicker);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
测试代码
None.gif private   void  Form_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            InitForm( dataGrid1 );
InBlock.gif
InBlock.gif            DataTable namesDataTable 
= new DataTable("NamesTable");
InBlock.gif
InBlock.gif            namesDataTable.Columns.Add(
"Name",typeof(string));
InBlock.gif            namesDataTable.Columns.Add(
"Date"typeof(DateTime));
InBlock.gif
InBlock.gif            DataSet namesDataSet 
= new DataSet();
InBlock.gif            namesDataSet.Tables.Add(namesDataTable);
InBlock.gif            dataGrid1.DataSource 
= namesDataSet;
InBlock.gif            dataGrid1.DataMember 
= "NamesTable";
InBlock.gif
InBlock.gif            
//添加样式
InBlock.gif
            AddGridStyle( dataGrid1 );
InBlock.gif            AddData( namesDataTable );
InBlock.gif
InBlock.gif            
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  AddGridStyle( DataGrid grid)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataGridTableStyle myGridStyle 
= new DataGridTableStyle();
InBlock.gif            myGridStyle.GridColumnStyles.Clear();
InBlock.gif            myGridStyle.MappingName 
= "NamesTable";
InBlock.gif
InBlock.gif            DataGridTextBoxColumn nameColumnStyle 
= new DataGridTextBoxColumn();
InBlock.gif            nameColumnStyle.MappingName 
= "Name";
InBlock.gif            nameColumnStyle.HeaderText
= "Name";
InBlock.gif            myGridStyle.GridColumnStyles.Add(nameColumnStyle);
InBlock.gif
InBlock.gif            DataGridTimePickerColumn timePickerColumnStyle 
= new DataGridTimePickerColumn();
InBlock.gif            timePickerColumnStyle.MappingName 
= "Date";
InBlock.gif            timePickerColumnStyle.HeaderText 
= "Date";
InBlock.gif            timePickerColumnStyle.Width 
= 100;
InBlock.gif            myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);
InBlock.gif
InBlock.gif            grid.TableStyles.Add(myGridStyle);
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  AddData( DataTable namesDataTable ) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataRow dRow 
= namesDataTable.NewRow();
InBlock.gif            dRow[
"Name"= "小站";
InBlock.gif            dRow[
"Date"= new DateTime(20011201);
InBlock.gif            namesDataTable.Rows.Add(dRow);
InBlock.gif
InBlock.gif            dRow 
= namesDataTable.NewRow();
InBlock.gif            dRow[
"Name"= "小李";
InBlock.gif            dRow[
"Date"= new DateTime(20011204);
InBlock.gif            namesDataTable.Rows.Add(dRow);
InBlock.gif
InBlock.gif            dRow 
= namesDataTable.NewRow();
InBlock.gif            dRow[
"Name"= "小吴";
InBlock.gif            dRow[
"Date"= new DateTime(20011229);
InBlock.gif            namesDataTable.Rows.Add(dRow);
InBlock.gif
InBlock.gif            namesDataTable.AcceptChanges();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  InitForm( DataGrid grid)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            grid.TabStop 
= true;
InBlock.gif            grid.TabIndex 
= 1;
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/timsoft/articles/413966.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值