【问题标题】:How to enable NULL value selection in DataGridview CalendarColumn如何在 DataGridview CalendarColumn 中启用 NULL 值选择
【发布时间】:2011-03-13 04:16:44
【问题描述】:
我有一个 DataGridView CalendarColumn。默认情况下,如果它在数据库表中绑定的列是 NULL,它会显示当前的,但我也有一个要求让它也只是 NULL。
例如,如果该特定日期列的数据,我希望用户能够将日期单元格设为空(NULL),但我找不到要设置的属性来实现这一点。任何想法,都可以在 Datagridview 的日历列中实现这种行为
【问题讨论】:
标签:
datagridview
datagridviewcolumn
【解决方案1】:
ctl.ShowCheckBox = this.isNullable;
if (this.Value != null && this.Value != DBNull.Value) {
if (this.Value is DateTime) {
ctl.Value = (DateTime)this.Value;
} else {
DateTime dtVal;
if (DateTime.TryParse(Convert.ToString(this.Value), out dtVal)) ctl.Value = dtVal;
}
if (this.isNullable) ctl.Checked = true;
} else if (this.isNullable) {
ctl.Checked = false;
}
【解决方案2】:
public object EditingControlFormattedValue {
get {
if (this.ShowCheckBox && !this.Checked) {
return String.Empty;
} else {
if (this.Format == DateTimePickerFormat.Custom) {
return this.Value.ToString();
} else {
return this.Value.ToShortDateString();
}
}
}
set {
string newValue = value as string;
if (!String.IsNullOrEmpty(newValue)) {
this.Value = DateTime.Parse(newValue);
} else if (this.ShowCheckBox) {
this.Checked = false;
}
}
}