已有56人关注
DataGridView控件添加日期列
发表在C#图书答疑 2010-10-26
是否精华
版块置顶:
老师:
    您好,WinForm窗体中的DataGridView控件绑定了dataset后,我想在datagridview的每行手动添加一下选择日期的列,请问我应该怎么做?
分享到:
精彩评论 1
东方_mrkj
学分:0 LV1
2010-10-26
沙发
读者你好:
    请下参考下面的源代码:
//定义日历列
public class CalendarCell : DataGridViewTextBoxCell   
    {   
  
        public CalendarCell()   
            : base()   
        {   
            // Use the short date format.   
            this.Style.Format = "d";   
        }   
  
        public override void InitializeEditingControl(int rowIndex, object  
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)   
        {   
            // Set the value of the editing control to the current cell value.   
            base.InitializeEditingControl(rowIndex, initialFormattedValue,   
                dataGridViewCellStyle);   
            CalendarEditingControl ctl =   
                DataGridView.EditingControl as CalendarEditingControl;   
            if (this.Value == null)   
                ctl.Value = DateTime.Now;   
            else  
                ctl.Value = (DateTime)this.Value;   
        }   
  
        public override Type EditType   
        {   
            get  
            {   
                // Return the type of the editing contol that CalendarCell uses.   
                return typeof(CalendarEditingControl);   
            }   
        }   
  
        public override Type ValueType   
        {   
            get  
            {   
                // Return the type of the value that CalendarCell contains.   
                return typeof(DateTime);   
            }   
        }   
  
        public override object DefaultNewRowValue   
        {   
            get  
            {   
                // Use the current date and time as the default value.   
                return DateTime.Now;   
            }   
        }   
    }   
  
    class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl   
    {   
        DataGridView dataGridView;   
        private bool valueChanged = false;   
        int rowIndex;   
  
        public CalendarEditingControl()   
        {   
            this.Format = DateTimePickerFormat.Short;   
        }   
  
        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue    
        // property.   
        public object EditingControlFormattedValue   
        {   
            get  
            {   
                return this.Value.ToShortDateString();   
            }   
            set  
            {   
                String newValue = value as String;   
                if (newValue != null)   
                {   
                    this.Value = DateTime.Parse(newValue);   
                }   
            }   
        }   
  
        // Implements the    
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.   
        public object GetEditingControlFormattedValue(   
            DataGridViewDataErrorContexts context)   
        {   
            return EditingControlFormattedValue;   
        }   
  
        // Implements the    
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.   
        public void ApplyCellStyleToEditingControl(   
            DataGridViewCellStyle dataGridViewCellStyle)   
        {   
            this.Font = dataGridViewCellStyle.Font;   
            this.CalendarForeColor = dataGridViewCellStyle.ForeColor;   
            this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;   
        }   
  
        // Implements the IDataGridViewEditingControl.EditingControlRowIndex    
        // property.   
        public int EditingControlRowIndex   
        {   
            get  
            {   
                return rowIndex;   
            }   
            set  
            {   
                rowIndex = value;   
            }   
        }   
  
        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey    
        // method.   
        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 false;   
            }   
        }   
  
        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit    
        // method.   
        public void PrepareEditingControlForEdit(bool selectAll)   
        {   
            // No preparation needs to be done.   
        }   
  
        // Implements the IDataGridViewEditingControl   
        // .RepositionEditingControlOnValueChange property.   
        public bool RepositionEditingControlOnValueChange   
        {   
            get  
            {   
                return false;   
            }   
        }   
  
        // Implements the IDataGridViewEditingControl   
        // .EditingControlDataGridView property.   
        public DataGridView EditingControlDataGridView   
        {   
            get  
            {   
                return dataGridView;   
            }   
            set  
            {   
                dataGridView = value;   
            }   
        }   
  
        // Implements the IDataGridViewEditingControl   
        // .EditingControlValueChanged property.   
        public bool EditingControlValueChanged   
        {   
            get  
            {   
                return valueChanged;   
            }   
            set  
            {   
                valueChanged = value;   
            }   
        }   
  
        // Implements the IDataGridViewEditingControl   
        // .EditingPanelCursor property.   
        public Cursor EditingPanelCursor   
        {   
            get  
            {   
                return base.Cursor;   
            }   
        }   
  
        protected override void OnValueChanged(EventArgs eventargs)   
        {   
            // Notify the DataGridView that the contents of the cell   
            // have changed.   
            valueChanged = true;   
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);   
            base.OnValueChanged(eventargs);   
        }   
    }  



   //调用和DataGridViewTextBoxColumn一样
    private CalendarColumn awardsDate;
    this.awardsDate = new CalendarColumn();
    this.awardsDate.DataPropertyName = "awardsDate";
    this.awardsDate.HeaderText = "颁奖日期";
    this.awardsDate.Name = "awardsDate";
    this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.awardsDate});
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照