我现在有1列是时间类型我想让这一列的cell都变成时间控件可不可以,应该怎么实现。。
解决方案 »
- 为什么截屏发送图片的时候出现错误?大家帮忙看一下。。。。
- c#中调用DLL(C++)以显示多行字符串的问题!
- C#.net Web2005开发中定时器的使用。
- DateGridView读取其中一行的一个单元格的值后失去焦点怎么回事
- httphanlder类中使用Server.Transfer定向到另外一个页面的错误,200分问题。
- string strSQL="select * from 表 where 姓名='"+name+"'"and 密码="'"+pw+"'";
- 关于 MDI窗口背景图片
- 求教,C#关于发送智能卡命令的操作问题
- 如何在关闭窗体时,同时释放它所占用的内存空间!
- 请问:写一个pop3收邮件程序应该用什么控件?
- 同一个解决方案中建立了两个项目A和B,在A中存放有主窗体,B中存放子窗体,在A中的主窗体打开B中的子窗体,我现在需要在打开的子窗体中去操作主窗体,不知如何做?
- 关于dataset在内存中的周期的问题!
参考代码:
public class CalendarColumn : DataGridViewColumn
{
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;
}
}
} 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;
ctl.Value = Convert.ToDateTime(this.Value.ToString());
} public override Type EditType
{
get
{
return typeof(CalendarEditingControl);
}
} public override Type ValueType
{
get
{
return typeof(DateTime);
}
} public override object DefaultNewRowValue
{
get
{
DateTime a = Convert.ToDateTime("00:00:00");
return a;
}
}
} class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{ DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex; public CalendarEditingControl()
{
// this.Format = DateTimePickerFormat.Time;
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = "HH:mm";
this.ShowUpDown = true;
}
public object EditingControlFormattedValue
{
get
{
return this.Value.ToLongTimeString();
}
set
{
String newValue = value as String; if (newValue != null)
{ this.Value = Convert.ToDateTime(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);
}
}
然后在绘制单元格事件的时候将控件覆盖到该列再就是重写一个datagridview的列
這個裡面就有你想要的,我個人覺得還不錯!你試試看!
if(dataGridView1.Columns[i].ValueType == typeof(DateTime))
{
//该列所有单元格配置成时间控件
}