创建可为空的Datagridview DateTime列

时间:2012-02-28 08:56:29

标签: c# winforms datagridview

我正在编写一个datagriview列来处理输入空值的用户(在这种情况下,如果没有为用户设置离开日期,则将其存储为null并显示为N / A)

我几乎已经使用可以为空的datetimepicker工作,但是网格没有获得更改为空值,它只是切换回旧时间值。当行初始化时,它以空值开始,显示正常。

然后将数据绑定回模型类

日历单元格

public class CtlDataGridViewNullableCalendarCell : DataGridViewTextBoxCell
{

public CtlDataGridViewNullableCalendarCell()
    : base()
{
    this.Style.Format = "d";
}

public override void InitializeEditingControl(int rowIndex, object
    initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
    // Set the value of the editing control to the current cell value.
    base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
    CtlCalendarNullableEditingControl ctl = DataGridView.EditingControl as CtlCalendarNullableEditingControl;
    ctl.Value = (DateTime?)this.Value;

    // a hacky way of getting the DateTimePicker to reset its focus, rather than remembering which date part was previously focussed
    DateTimePickerFormat format = ctl.Format;
    ctl.Format = DateTimePickerFormat.Custom;
    ctl.Format = format;
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
    if (formattedValue == null)
        return null;
    else
        return (formattedValue as DateTime?).Value.Date;
}
public override Type EditType
{
    get
    {
        // Return the type of the editing control that CalendarCell uses.
        return typeof(CtlCalendarNullableEditingControl);
    }
}

public override Type ValueType
{
    get
    {
        // Return the type of the value that CalendarCell contains.
        return typeof(DateTime?);
    }
}

public override object DefaultNewRowValue
{
    get
    {
        // Use the current date as the default value.
        return null;
    }
}

#endregion
}

编辑控件的覆盖

class CtlCalendarNullableEditingControl : CtlNullableDateTimePicker, IDataGridViewEditingControl
{
DataGridView _dataGridView;
private bool _valueChanged = false;
int _rowIndex;

public object EditingControlFormattedValue
{
    get
    {
        return this.Value;
    }
    set
    {
        if (value is string)
            if ((string)value == string.Empty)
                value = null;
            else
                value = DateTime.Parse((string)value);

    }
}

public object GetEditingControlFormattedValue(
    DataGridViewDataErrorContexts context)
{
    return EditingControlFormattedValue as DateTime?;
}

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)
{
    // Let the DateTimePicker handle the keys listed.
    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:
        case Keys.Delete:
        case Keys.Back:
            return true;
        default:
            return false;
    }
}

public DataGridView EditingControlDataGridView
{
    get
    {
        return _dataGridView;
    }
    set
    {
        _dataGridView = value;
    }
}

public bool EditingControlValueChanged
{
    get
    {
        return _valueChanged;
    }
    set
    {
        _valueChanged = value;
    }
}


#region -- DateTimePicker overrides --

/// <summary>
/// Handle the OnValueChanged event from the <see cref="DateTimePicker"/> and ensure the change propagates to the grid.
/// </summary>
/// <param name="eventargs"></param>
protected override void OnValueChanged(EventArgs eventargs)
{
    // Notify the DataGridView that the contents of the cell have changed.
    _valueChanged = true;
    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
    base.OnValueChanged(eventargs);
}

编辑控件完全适用于它自己,所以我确定问题存在于实际的单元类中,但我不熟悉winforms以发现问题。

我会非常感激地收到建议甚至指点。

2 个答案:

答案 0 :(得分:0)

对null值的更改实际上是= NULL,还是为空?

在线上放置一个断点:

if ((string)value == string.Empty)

if (formattedValue == null)

查看value和formattedValue是否符合您的预期。

答案 1 :(得分:0)

public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle,TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
  if (formattedValue != DBNull.Value && formattedValue.ToString() != "")
  {
    return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
  }
  else
  {
    return DBNull.Value;
  }
}