如何在一个DataGridView中的一列添加DateTimePicker控件

kingmaple 2007-01-15 03:28:23
如何在一个DataGridView中的一列上添加DateTimePicker控件,
...全文
947 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
BrightFire 2010-08-10
  • 打赏
  • 举报
回复
DateTimePicker貌似没办法调整高度的,所以也就不能覆盖整个单元格
firein 2008-03-31
  • 打赏
  • 举报
回复
学习
Arthur_qi 2007-10-12
  • 打赏
  • 举报
回复
顶~!
yuanzhihua520 2007-03-02
  • 打赏
  • 举报
回复
up
teachman_999 2007-01-15
  • 打赏
  • 举报
回复
class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public CalendarEditingControl()
{

}

public object EditingControlFormattedValue
{
get
{
return this.Value.ToLongDateString();
}
set
{
String newValue = value as String;
if (newValue != null)
{
this.Value = DateTime.Parse(newValue);
}
}
}


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
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{

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;
}
}


public void PrepareEditingControlForEdit(bool selectAll)
{

}

public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}

public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}

public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}

protected override void OnValueChanged(EventArgs eventargs)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
teachman_999 2007-01-15
  • 打赏
  • 举报
回复
public class CalendarCell : DataGridViewTextBoxCell
{
public CalendarCell()
: base()
{

}

public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{


base.InitializeEditingControl(rowIndex, initialFormattedValue,dataGridViewCellStyle);
CalendarEditingControl ctl =
DataGridView.EditingControl as CalendarEditingControl;
ctl.Text = this.Value.ToString();

}

public override Type EditType
{
get
{

return typeof(CalendarEditingControl);
}
}

public override Type ValueType
{
get
{

return typeof(DateTime);
}
}

public override object DefaultNewRowValue
{
get
{

return DateTime.Now;
}
}
}
teachman_999 2007-01-15
  • 打赏
  • 举报
回复
你建个组件类,下面是源码,自已再贴上去

public class CalendarColumn : DataGridViewColumn
{

private bool showUpDown = true;

public bool ShowUpDown
{
get
{
return showUpDown;
}
set
{
showUpDown = value;
}
}

public CalendarColumn()
: base(new CalendarCell())
{
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{

if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CalendarCell)))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}

private void InitializeComponent()
{

}
}

北京的雾霾天 2007-01-15
  • 打赏
  • 举报
回复
MSDN:
您可以通过继承 DataGridViewColumn 类或该类的任何一个派生类来创建自己的列类,从而提供自定义的外观、行为或寄宿控件。有关更多信息,请参见如何:通过扩展 Windows 窗体 DataGridView 控件中单元格和列的行为和外观对其进行自定义
北京的雾霾天 2007-01-15
  • 打赏
  • 举报
回复
MSDN上其实有了这样的例子了,楼主找下看看.
北京的雾霾天 2007-01-15
  • 打赏
  • 举报
回复
这个要自定义了,可能用到如下的几个类:
从DataGridViewColumn继承一个新的列类型
从DataGridViewCell继承一个新的单元类型.
从DateTimePicker, IDataGridViewEditingControl继承新的DateTimePicker控件,

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧