我正在研究自定义DataGridViewColumn
,允许用户输入类似1:30
的值。
如果用户将其保存到Database
,则我有一个函数将其存储为十进制值,因此1:30的for将变为1.5。数据库列是float
。
现在,我的问题是输入1:30之类的时间格式时,单元格不接受。细胞失去焦点后,值消失。那么是否可以在类型为double的单元格中显示字符串格式(1:30)?怎么样?你能告诉我一些代码吗?谢谢。
自定义DataGridViewColumn
的代码:
public class OptTimeColumn : DataGridViewColumn
{
public OptTimeColumn() : base(new OptTimeCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(OptTimeCell)))
{
throw new InvalidCastException("Must be a OptTimeCell");
}
base.CellTemplate = value;
}
}
}
public class OptTimeCell : DataGridViewTextBoxCell
{
public OptTimeCell() : base()
{
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
OptTimeColumnEditingControl ctl = DataGridView.EditingControl as OptTimeColumnEditingControl;
OptTimeColumn col = (OptTimeColumn)this.OwningColumn;
if (this.Value == null)
{
ctl.DecimalValue = Convert.ToDouble(this.DefaultNewRowValue);
}
else
{
ctl.DecimalValue = Convert.ToDouble(this.Value);
}
}
public override Type EditType
{
get
{
return typeof(OptTimeColumnEditingControl);
}
}
public override Type ValueType
{
get
{
//return typeof(string);
return typeof(double);
}
}
public override object DefaultNewRowValue
{
get
{
//return string.Empty;
return DBNull.Value;
}
}
}
public class OptTimeColumnEditingControl : OptTime.OptTime, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public OptTimeColumnEditingControl()
{
}
public object EditingControlFormattedValue
{
get
{
return this.DecimalValue;
}
set
{
if (value is double)
{
try
{
this.DecimalValue = Convert.ToDouble(value);
}
catch
{
this.DecimalValue = 0;
}
}
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
}
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 !dataGridViewWantsInputKey;
}
}
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 OnTextChanged(EventArgs eventargs)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(eventargs);
}
}
答案 0 :(得分:0)
Why-oh-why are you saving time to db as double?
您的数据库不支持Time或DateTime数据类型吗?