How to enable NULL value selection in DataGridview CalendarColumn

喜欢而已 提交于 2019-12-11 05:43:54

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/3201058/how-to-enable-null-value-selection-in-datagridview-calendarcolumn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!