C# 对于c windows窗体中DataGridView控件内的Datetimepicker,从EditMode返回ViewMode

C# 对于c windows窗体中DataGridView控件内的Datetimepicker,从EditMode返回ViewMode,c#,winforms,C#,Winforms,我在DataGridView的一个单元格中有DateTimepicker。编辑日期后,如果用户按escape键,我可以将单元格的值还原为旧值,但datetimepicker仍处于编辑模式。我想让单元格在viewmode中,就像第一次加载表单时一样 下面是我的代码 public bool CellEditCanceled { get; set; } private void gvInstanceDetails_CellBeginEdit(object sen

我在DataGridView的一个单元格中有DateTimepicker。编辑日期后,如果用户按escape键,我可以将单元格的值还原为旧值,但datetimepicker仍处于编辑模式。我想让单元格在viewmode中,就像第一次加载表单时一样

下面是我的代码

public bool CellEditCanceled
    {
      get;
      set;
    }
private void gvInstanceDetails_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
      CellEditCanceled = false;
      if (gvInstanceDetails.Focused && gvInstanceDetails.CurrentCell.ColumnIndex == 5)
      {
        oDateTimePicker.Location = gvInstanceDetails.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
        oDateTimePicker.Visible = true;
        if (gvInstanceDetails.CurrentCell.Value != null)
        {
          oldDateValue= (DateTime)gvInstanceDetails.CurrentCell.Value;
          oDateTimePicker.Value = (DateTime)gvInstanceDetails.CurrentCell.Value;
        }
        else
        {
          oDateTimePicker.Value = DateTime.Today;
        }
      }
      else
      {
        oDateTimePicker.Visible = false;
      }
    }

private void gvInstanceDetails_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
      if (gvInstanceDetails.Focused && gvInstanceDetails.CurrentCell.ColumnIndex == 5)
      {
        if (CellEditCanceled)
        {
          oDateTimePicker.Value = oldDateValue;
        }
        gvInstanceDetails.CurrentCell.Value = oDateTimePicker.Value;

      }      
    }    
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
      Keys key = keyData & Keys.KeyCode;
      if (gvInstanceDetails.CurrentCell != null)
      {
        if (gvInstanceDetails.CurrentCell.IsInEditMode)
        {
          switch (key)
          {
            case Keys.Escape:
              CellEditCanceled = true;
              break;
          }
        }
      }
      return base.ProcessCmdKey(ref msg, keyData);
    }

退出按钮后,datetimepicker保持如下状态,但我希望它返回到查看模式。

根据要求,这是我的测试代码:

MainForm除了在构造函数中创建的DataGridView之外没有其他内容。DateTimePicker功能是使用DataGridViews通用列框架创建的,用于创建自定义单元格/列类型

运行测试应用程序时,我按F2键开始编辑,使用光标键更改值,然后按ESC键取消编辑,然后返回原始值,并从网格中删除编辑控件,只留下网格自己的显示文本框

MainForm.cs

DataGridViewDateTimeCell.cs

DataGridViewDateTimeColumn.cs

DataGridViewDateTimeEditingControl.cs


我用一个空的/干净的/新的DataGridView和两行DataGridViewDateTimeColumn=new DataGridViewDateTimeColumn测试了这一点;dataGridView1.Columns.Addcol;。一切都像你描述的那样。这可能是因为您过度设计了解决方案,从而造成了问题。如果你愿意,我很乐意与你分享我的全部测试代码。@JayV:你能分享一下你的测试代码吗。我将检查如何实现该方法。Thanks@JayV当前位置我能按照你的建议做这件事。不需要共享代码。谢谢
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        DataGridView dataGridView1 = new DataGridView();
        this.Controls.Add(dataGridView1);

        dataGridView1.Columns.Clear();
        DataGridViewDateTimeColumn col = new DataGridViewDateTimeColumn();
        dataGridView1.Columns.Add(col);
    }
}
public class DataGridViewDateTimeCell : DataGridViewTextBoxCell
{
    Object _defaultNewRowValue;
    Boolean _defaultNewRowValueSet;

    public DataGridViewDateTimeCell(Object defaultNewRowValue) : base()
    {
        _defaultNewRowValueSet = true;
        _defaultNewRowValue = defaultNewRowValue;
    }

    public DataGridViewDateTimeCell() : this(DateTime.Now)
    {
        _defaultNewRowValueSet = false;
    }

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        try
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            DataGridViewDateTimeEditingControl ctl = DataGridView.EditingControl as DataGridViewDateTimeEditingControl;

            if (this.Value != DBNull.Value && this.Value != null)
            {
                ctl.Value = Convert.ToDateTime(this.Value);
            }
        }
        catch
        {
            // Do nothing with the exception
        }
    }

    // Return the type of the editing contol that CalendarCell uses.
    public override Type EditType { get { return typeof(DataGridViewDateTimeEditingControl); } }

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

    public override object DefaultNewRowValue
    {
        get
        {
            if (_defaultNewRowValueSet)
            {
                // Use the value given to us as the Default New Row Value
                return _defaultNewRowValue;
            }
            else
            {
                // Use the current date and time as the default value.
                return DateTime.Now;
            }
        }
    }

    public override Object Clone()
    {
        DataGridViewDateTimeCell retVal = base.Clone() as DataGridViewDateTimeCell;
        retVal._defaultNewRowValueSet = this._defaultNewRowValueSet;
        retVal._defaultNewRowValue = this._defaultNewRowValue;

        return retVal;
    }
}
public class DataGridViewDateTimeColumn : DataGridViewColumn
{
    public DataGridViewDateTimeColumn() : base(new DataGridViewDateTimeCell())
    {
        base.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        base.SortMode = DataGridViewColumnSortMode.Automatic;
    }

    public DataGridViewDateTimeColumn(Object defaultNewRowValue) : base(new DataGridViewDateTimeCell(defaultNewRowValue))
    {
        // Does nothing
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a DataGridViewDateTimeCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(DataGridViewDateTimeCell)))
            {
                throw new InvalidCastException("Must be a DataGridViewDateTimeCell");
            }
            base.CellTemplate = value;
        }
    }
}
public class DataGridViewDateTimeEditingControl : DateTimePicker, IDataGridViewEditingControl
{
    public DataGridViewDateTimeEditingControl()
    {
        this.Format = DateTimePickerFormat.Short;
    }

    // Implements the IDataGridViewEditingControl.EditingControlFormattedValue property.
    public object EditingControlFormattedValue
    {
        get { return this.Value.ToShortDateString(); }
        set
        {
            if (value is String)
            {
                this.Value = DateTime.Parse((String)value);
            }
        }
    }

    // Implements the IDataGridViewEditingControl.GetEditingControlFormattedValue method.
    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    // Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
        this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;

        if (dataGridViewCellStyle.Format != "d")
        {
            this.Format = DateTimePickerFormat.Custom;
            this.CustomFormat = dataGridViewCellStyle.Format;
        }
    }

    // Implements the IDataGridViewEditingControl.EditingControlRowIndex property.
    public int EditingControlRowIndex { get; set; }

    // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method.
    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.Tab:
                return true;
            default:
                return false;
        }
    }

    // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit method.
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    // Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange property.
    public bool RepositionEditingControlOnValueChange { get { return false; } }

    // Implements the IDataGridViewEditingControl.EditingControlDataGridView property.
    public DataGridView EditingControlDataGridView { get; set; }

    // Implements the IDataGridViewEditingControl.EditingControlValueChanged property.
    public bool EditingControlValueChanged { get; set; }

    // Implements the IDataGridViewEditingControl.EditingPanelCursor property.
    public Cursor EditingPanelCursor { get { return base.Cursor; } }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        // Notify the DataGridView that the contents of the cell have changed.
        EditingControlValueChanged = true;

        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);

        base.OnKeyPress(e);
    }

    protected override void OnCloseUp(EventArgs eventargs)
    {
        // Notify the DataGridView that the contents of the cell have changed.
        EditingControlValueChanged = true;

        // setting Value for date if format is Month and Year only
        if (!string.IsNullOrEmpty(this.CustomFormat))
        {
            if (this.CustomFormat.IndexOf("d") < 0)
            {
                this.Value = new DateTime(this.Value.Year, this.Value.Month, DateTime.DaysInMonth(this.Value.Year, this.Value.Month));
            }
        }

        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnCloseUp(eventargs);
    }

    protected override void OnValueChanged(EventArgs eventargs)
    {
        // Notify the DataGridView that the contents of the cell have changed.
        EditingControlValueChanged = true;

        // setting Value for date if format is Month and Year only
        if (!string.IsNullOrEmpty(this.CustomFormat))
        {
            if (this.CustomFormat.IndexOf("d") < 0)
            {
                this.Value = new DateTime(this.Value.Year, this.Value.Month, DateTime.DaysInMonth(this.Value.Year, this.Value.Month));
            }
        }

        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnValueChanged(eventargs);
    }
}