本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《 阿里云开发者社区用户服务协议 》和 《 阿里云开发者社区知识产权保护指引 》。如果您发现本社区中有涉嫌抄袭的内容,填写 侵权投诉表单 进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。 /// <summary> /// DataGridView自定义添加DateTimePicker控件日期列 参考http://msdn.microsoft.com/en-us/library/7tas5c80.aspx /// 涂聚文 缔友计算机信息技术有限公司 /// 2011-11-16 捷为工作室 /// </summary> public cla
/// <summary>
 /// DataGridView自定义添加DateTimePicker控件日期列 参考http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
 /// 涂聚文 缔友计算机信息技术有限公司
 /// 2011-11-16 捷为工作室
 /// </summary>
 public class GeovinDuCalendarColumn : DataGridViewColumn
     /// <summary>
     /// </summary>
     public GeovinDuCalendarColumn()
         : base(new CalendarCell())
     /// <summary>
     /// </summary>
     public override DataGridViewCell CellTemplate
             return base.CellTemplate;
             if (value != null &&
                 !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
                 throw new InvalidCastException("Must be a CalendarCell");
             base.CellTemplate = value;
 /// <summary>
 /// DataGridView 添加日期列
 /// 涂聚文 缔友计算机信息技术有限公司
 /// 2011-11-16 捷为工作室
 /// </summary>
 public class CalendarCell : DataGridViewTextBoxCell
     public CalendarCell()
         : base()
         this.Style.Format = "d";
     public override void InitializeEditingControl(int rowIndex, object
         initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
         base.InitializeEditingControl(rowIndex, initialFormattedValue,
             dataGridViewCellStyle);
         CalendarEditingControl ctl =
             DataGridView.EditingControl as CalendarEditingControl;
         if (this.Value == null)
             ctl.Value = (DateTime)this.DefaultNewRowValue;
             ctl.Value = (DateTime)this.Value;
     public override Type EditType
             return typeof(CalendarEditingControl);
     public override Type ValueType
             return typeof(DateTime);
     public override object DefaultNewRowValue
             return DateTime.Now;
 /// <summary>
 ///DataGridView 添加日期列
 /// 涂聚文 缔友计算机信息技术有限公司
 /// 2011-11-16 捷为工作室
 /// </summary>
 class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
     DataGridView dataGridView;
     private bool valueChanged = false;
     int rowIndex;
     public CalendarEditingControl()
         this.Format = DateTimePickerFormat.Short;
     public object EditingControlFormattedValue
             return this.Value.ToShortDateString();
             if (value is String)
                     this.Value = DateTime.Parse((String)value);
                 catch
                     this.Value = DateTime.Now;
     public object GetEditingControlFormattedValue(
         DataGridViewDataErrorContexts context)
         return EditingControlFormattedValue;
     public void ApplyCellStyleToEditingControl(
         DataGridViewCellStyle dataGridViewCellStyle)
         this.Font = dataGridViewCellStyle.Font;
         this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
         this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
     public int EditingControlRowIndex
             return rowIndex;
             rowIndex = value;
     public bool EditingControlWantsInputKey(
         Keys key, bool dataGridViewWantsInputKey)
         // Let the DateTimePicker handle the keys listed.
         switch (key & Keys.KeyCode)
             case Keys.Left:
             case Keys.Up:
             case Keys.Down:
             case Keys.Right:
             case Keys.Home:
             case Keys.End:
             case Keys.PageDown:
             case Keys.PageUp:
                 return true;
             default:
                 return !dataGridViewWantsInputKey;
     public void PrepareEditingControlForEdit(bool selectAll)
         // No preparation needs to be done.
     public bool RepositionEditingControlOnValueChange
             return false;
     public DataGridView EditingControlDataGridView
             return dataGridView;
             dataGridView = value;
     public bool EditingControlValueChanged
             return valueChanged;
             valueChanged = value;
     public Cursor EditingPanelCursor
             return base.Cursor;
     protected override void OnValueChanged(EventArgs eventargs)
         valueChanged = true;
         this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
         base.OnValueChanged(eventargs);
           //dataGridView1.Columns[3].HeaderText = "入職日期";
           GeovinDuCalendarColumn col = new GeovinDuCalendarColumn();
           this.dataGridView1.Columns.Insert(3,col);
           col.HeaderText = "入職日期";
/// <summary>
       /// 设置默认值
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
           if (dataGridView1.Rows.Count >= 1) //设定默认日期
               this.dataGridView1.Rows[e.RowIndex].Cells[2].Value = "20E8C162-C09C-4F7A-9C97-0CA50E201F6B";
               this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;
               this.dataGridView1.Rows[e.RowIndex].Cells[4].Value = "C50E08D5-7529-4F86-966E-9497AD67EA0C";
明确单元格DOM结构
要想弄清楚formatter和styler属性是怎么工作的,首先要弄清楚datagrid组件内容单元格的DOM接口,注意,这里指的是内容单元格,不包括标题单元格,标题单元格的结构有所区别。我们所有内容单元格的默认DOM结构如下:
    c#DataGridView数据绑定示例 格式化单元格的内容
在使用DataGridView显示数据库中的数据时,我们需要对某列的数据显示格式进行格式化。
这里使用实时构建的数据,如下图:
在显示时对第三列的数据进行格式化,如下图:
测试数据构建及数据绑定:
        priv...