How to enable NULL value selection in DataGridview

2019-08-10 01:10发布

I have a DataGridView CalendarColumn. By default, if the column its bound to in the database table is NULL, its shows the current, but i have a requirements to make it just NULL as well.

For example if the data for that particular date column, i want a user to be able to just make the Date Cell empty(NULL) but i cant find a property to set to achieve this. Any ideas, is to possible to achieve this kind of behaviour in a Datagridview's Calendercolumn

2条回答
Viruses.
2楼-- · 2019-08-10 02:08
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;
        }
    }
}
查看更多
冷血范
3楼-- · 2019-08-10 02:14
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;
}
查看更多
登录 后发表回答