我有一个DataGridView
与 aDataGridViewComboBoxColumn
绑定的 a List<IBrand>
。在此组合框列中,我允许用户选择现有值或键入新值。当用户选择现有值时,IsCurrentRowDirty()
正确返回 true。当用户输入一个值时,IsCurrentRowDirty()
当它显然应该返回 true 时,总是返回 false。
我已经尝试DataGridView.CommitEdit()
在CurrentCellDirtyStateChanged
事件中调用,但这不起作用
如何让用户在 a 中输入值DataGridViewComboBoxColumn
以将行设置为脏?
相关代码如下。
谢谢,
凯尔
public void BindBrands()
{
DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvReference.Columns[(int)ReferenceColumnsIndex.Brand];
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Self"; //"Self" returns "this"
comboBox.DataSource = brands;
}
//This enables the DataGridViewComboBoxColumn to be editable by the user
private void dgvReference_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewComboBoxColumn column = GetColumn<DataGridViewComboBoxColumn>(dgvReference,
(int) ReferenceColumnsIndex.Brand);
if (column != null)
{
ComboBox comboBox = e.Control as ComboBox;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
}
}
}
private void dgvReference_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
switch (e.ColumnIndex)
{
case (int)ReferenceColumnsIndex.Brand:
DataGridViewComboBoxColumn comboBox = dgvReference.Columns[(int)ReferenceColumnsIndex.Brand] as DataGridViewComboBoxColumn;
if (e.ColumnIndex == comboBox.DisplayIndex)
{
IBrand brand = new Brand(e.FormattedValue.ToString());
if (!brands.Contains(brand))
{
//If the brand does not exist, add it to the datasource of the combobox
brands.Add(brand);
dgvReference.NotifyCurrentCellDirty(true); //<== THIS LINE FIXES THE ISSUE
}
//When setting the .Value to brand here, IsCurrentRowDirty() does not return true
dgvReference.Rows[e.RowIndex].Cells[(int)ReferenceColumnsIndex.Brand].Value = brand;
}
break;
}
}