Создание обнуляемого столбца Datagridview DateTime - PullRequest
1 голос
/ 28 февраля 2012

Я пишу столбец datagriview для обработки ввода пользователем нулевых значений (в этом случае, если для пользователя не установлена ​​дата отпуска, она сохраняется как нулевая и отображается как N / A)

У меня почти получилось, что он работает с помощью обнуляемого указателя даты и времени, но сетка не обнаруживает изменения в нулевое значение, она просто переключается на старое значение времени. Когда строка инициализируется, она начинается с нулевого значения, которое отображается нормально.

Затем данные возвращаются в класс моделей

Календарная ячейка

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 голосов
/ 26 июня 2014
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;
  }
}
0 голосов
/ 29 февраля 2012

Является ли изменение нулевого значения на самом деле = NULL или оно пустое?

Поместить точку разрыва в строки:

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

и

if (formattedValue == null)

, чтобы увидеть, ожидают ли значения и formattedValue то, что вы ожидаете.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...