我正在使用DateTimePicker
为DataGridView
修改一些具有自定义日历控件的代码。我设置了ShowCheckBox = true
,我试图强制它在用户点击复选框(通过更改日期的CustomFormat
)时将日期值更改为null,这有效。我的问题是,复选框上需要太多次点击才能更改DateFormat。我的基本CalendarColumn来自http://msdn.microsoft.com/en-us/library/7tas5c80.aspx,但不包含复选框,并且不允许空值。
我的OnValueChanged()
代码是:
protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
base.OnValueChanged(eventargs);
if (this.Checked)
{
this.Checked = true;
this.Format = DateTimePickerFormat.Short;
} else if (!this.Checked)
{
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
}
}
首先单击复选框(当选中它的值时)会使日期变灰,但不会触发OnValueChanged()
方法,第二次单击会将其设置为“已检查”,并触发事件,第3次点击将CustomFormat
设置为“”,因此应显示空白日期。
我做了一些研究,我认为我的问题与第一次点击时获得单元格的焦点有关,但是如果我将我的支票放入onGotFocus()
,只需点击一下/隐藏日期格式,但是当我取消选中复选框后点击另一个单元格时,它仍然保留CustomFormat
设置为“”而不是DateTimePickerFormat.Short
。
关于类似主题的其他答案引用http://www.mofeel.net/70-microsoft-public-dotnet-framework-windowsforms/8806.aspx,但我对如何将其合并到我的代码中感到困惑。我没有发布整个班级,但是如果有人认为这会有帮助吗?
答案 0 :(得分:1)
一位同事为我解决了这个问题。我将尽力解释:
我的OnValueChanged()
方法最终看起来像:
protected override void OnValueChanged(EventArgs eventargs)
{
valueChanged = true;
// Notify the DataGridView that the contents of the cell
// have changed.
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
isChecked();
}
public bool isChecked()
{
bool isChecked = false;
if (this.Checked)
{
this.Checked = true;
this.Format = DateTimePickerFormat.Short;
isChecked = true;
}
else if (!this.Checked)
{
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
isChecked = false;
}
return isChecked;
}
他还添加了以下方法:
protected override void OnClick(EventArgs e)
{
isChecked();
base.OnClick(e);
}
public object EditingControlFormattedValue
{
get
{
if (!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);
}
}
}
InitializaEditingControl
也被编辑为:
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);
ctl = DataGridView.EditingControl as CalendarEditingControl;
// ctl.Invalidated += new InvalidateEventHandler(ctl_Invalidated);
ctl.ValueChangedSpecial += new EventHandler(ctl_ValueChangedSpecial);
if (rowIndex >= 0)
{
try
{
if (String.IsNullOrEmpty(this.Value.ToString()))
{
ctl.Checked = false;
ctl.Format = DateTimePickerFormat.Custom;
ctl.CustomFormat = " ";
}
else
{
ctl.Checked = true;
ctl.Value = (DateTime)this.Value;
ctl.Format = DateTimePickerFormat.Short;
}
}
catch (ArgumentOutOfRangeException aex)
{
//MessageBox.Show("ERROR. " + aex.Message);
ctl.Value = (DateTime)this.DefaultNewRowValue;
}
}
}
然后它奏效了。
这个答案几乎肯定是不能令人满意的,但是没有人回答的问题无人问津,所以希望这里有些东西可以帮助别人。