我在 WinForms DataGridView 与数据绑定(bind)方面遇到了困难。我将包装 DataSet 的 DataView 分配给 DataGridView.DataSource,到目前为止它运行良好。实现自定义DataGridViewCell 时问题就开始了。我的目标是提供一个用于选择 Enum 值的 ComboBoxCell,它始终是完全交互的并且不需要用户明确进入编辑模式。
这是绑定(bind)设置:
- DataSet S恰好包含一个DataTable,T
- DataView V 包装所述表格
- DataGridView.DataSource 设置为 V
- 应用程序的某些部分订阅了 T.RowChanged 事件。这是关键部分。
就功能而言,我的自定义单元格的行为完全符合预期。但是,它不会导致触发 DataTable.RowChanged 事件,除非整个 DataGridView 失去焦点...但所有其他非自定义单元格都会。我仍然收到 CellValueChanged 事件,并且 DataSet 具有新值.. 但是既没有 DataTable.RowChanged 也没有 DataGridView.DataBindingComplete,并且该行不会像往常一样自动失效。
我显然做错了什么。我可能错过了一个通知程序事件或实现了一些错误的东西,但经过两天的搜索、单步执行和反汇编 .Net 代码后,我仍然完全被困住了。
以下是类定义中最重要的部分(不是完整的源代码):
public class DataGridViewEnumCell : DataGridViewCell, IDataGridViewEditingCell
{
private Type enumType = null;
private Enum enumValue = default(Enum);
private bool enumValueChanged = false;
public virtual object EditingCellFormattedValue
{
get { return this.GetEditingCellFormattedValue(DataGridViewDataErrorContexts.Formatting); }
set { this.enumValue = (Enum)Utility.SafeCast(value, this.enumType); }
}
public virtual bool EditingCellValueChanged
{
get { return this.enumValueChanged; }
set { this.enumValueChanged = value; }
}
public override Type EditType
{
get { return null; }
}
public override Type FormattedValueType
{
get { return this.enumType; }
}
public override Type ValueType
{
get
{
if (this.OwningColumn != null && this.OwningColumn.ValueType != null)
{
return this.OwningColumn.ValueType;
}
else
{
return this.enumType;
}
}
set
{
base.ValueType = value;
}
}
// The kind of Enum that is edited in this cell.
public Type EnumValueType
{
get { return this.enumType; }
set { this.enumType = value; }
}
public virtual object GetEditingCellFormattedValue(DataGridViewDataErrorContexts context)
{
if (context.HasFlag(DataGridViewDataErrorContexts.ClipboardContent))
{
return Convert.ToString(this.enumValue);
}
else
{
return this.enumValue ?? this.enumType.GetDefaultValue();
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
// Cast the Enum value to the original cell value type
object cellVal;
Utility.SafeCast(formattedValue, this.ValueType, out cellVal);
return cellVal;
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (this.DataGridView == null || value == null)
{
return this.enumType.GetDefaultValue();
}
// Cast the cell value to the appropriate Enum value type
object enumVal;
Utility.SafeCast(value, this.enumType, out enumVal);
// Let the base implementation apply additional formatting
return base.GetFormattedValue(enumVal, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
private Enum GetCurrentValue()
{
object unknownVal = (this.enumValueChanged ? this.enumValue : this.Value);
object enumVal;
Utility.SafeCast(unknownVal, this.enumType, out enumVal);
return (Enum)enumVal;
}
public virtual void PrepareEditingCellForEdit(bool selectAll)
{
this.enumValue = this.GetCurrentValue();
}
protected override void OnClick(DataGridViewCellEventArgs e)
{
base.OnClick(e);
if (this.DataGridView.CurrentCell == this && (DateTime.Now - this.mouseClosed).TotalMilliseconds > 200)
{
// Due to some reason I don't understand sometimes EditMode is already active.
// Don't do it twice in these cases.
if (!this.IsInEditMode)
{
// Begin editing
this.DataGridView.BeginEdit(true);
}
this.ShowDropDown();
}
}
public void HideDropDown()
{
// ... snip ...
// Revert value to original state, if not accepted explicitly
// It will also run into this code after the new selection
// has been accepted (see below)
if (this.DataGridView != null)
{
this.enumValue = this.GetCurrentValue();
this.enumValueChanged = false;
this.DataGridView.EndEdit();
}
}
// Called when a value has been selected. All calue changes run through this method!
private void dropdown_AcceptSelection(object sender, EventArgs e)
{
Enum selectedEnum = (Enum)this.dropdown.SelectedItem;
if (!this.enumValue.Equals(selectedEnum))
{
this.enumValue = selectedEnum;
this.enumValueChanged = true;
this.DataGridView.NotifyCurrentCellDirty(true);
this.DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
}
同样,当 DataGridView 失去焦点或编辑 DataGridView 中的任何其他单元格时,DataSource 会正确触发事件,但我无法在编辑自定义单元格后更新它.
我怎样才能做到这一点?
最佳答案
我终于能够解决这个问题。
事实证明,整个问题与我的自定义 IDataGridViewEditingCell 没有任何关系。没有收到 RowChanged 事件只是因为 DataGridView 行在离开当前选定的行之前未经过验证 - 我没有注意到,因为我的测试中只有一行,所以我不得不聚焦不同的控件来实现这一点。
在取消选择/散焦之前不验证当前行,这似乎是 DataGridView 中的预期行为和正常行为。但这不是我想要的,所以我派生了自己的 DataGridView,并执行了以下操作:
protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
{
base.OnCellEndEdit(e);
// Force validation after each cell edit, making sure that
// all row changes are validated in the DataSource immediately.
this.OnValidating(new System.ComponentModel.CancelEventArgs());
}
到目前为止,它似乎完美无缺,但我可能只是走运。非常感谢更有经验的 DataGridView 开发人员的任何批准,所以..请随时发表评论!
关于c# - 自定义 DataGridViewCell 不触发数据源更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21911036/