2
設置I具有與被結合到一個List<IBrand>
一個DataGridViewComboBoxColumn
DataGridView
。在這個組合框列中,我允許用戶選擇一個現有的值或鍵入一個新值。當用戶選擇現有值時,IsCurrentRowDirty()
正確返回true。當用戶鍵入一個值時,IsCurrentRowDirty()
顯然應該返回true時總是返回false。DataGridView.IsCurrentRowDirty()不具有可編輯DataGridViewComboBoxColumn
我已經打過電話在CurrentCellDirtyStateChanged
事件DataGridView.CommitEdit()
但不起作用
我如何在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;
}
}