我有一个带有一列组合框的DataGridView。这些组合框可以容纳1-24之间的数字。我在DGV中有8行,我想防止任意两行在组合框中选择相同的数字。如果更改的CB与任何其他CB发生冲突,则将更改后的CB设置回存储在“ CorrectSlotSelections”中的上一个值。如果更改有效且没有冲突,我将更新存储在“ CorrectSlotSelections”中的值。
这一切都正常工作,但是,即使我覆盖了不正确的值,如果发现它无效,屏幕上显示的值也不会更新。在调试模式下检查DGV时,我可以看到我已经过时地覆盖了该值,但是属性“ EditedFormattedValue”仍然显示旧的,错误的值。如何更改此设置以更新UI,以显示更正后的值?
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (this.ChannelConfigDataGridView.IsCurrentCellDirty)
{
// This fires the cell value changed handler below
ChannelConfigDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void ChannelConfigDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)ChannelConfigDataGridView.Rows[e.RowIndex].Cells[2];
if (cb.Value != null)
{
// Check all rows for a cell in column 3 that has the value that was just changed.
List<DataGridViewRow> rowsWithDuplicateValue = ChannelConfigDataGridView.Rows.Cast<DataGridViewRow>().Where(x => (int)x.Cells[2].Value == (int)cb.Value).ToList();
if (rowsWithDuplicateValue == null || rowsWithDuplicateValue.Count() == 0)
{
return;
}
// If more than one item with this value was found we must handle the duplicates.
if (rowsWithDuplicateValue.Count() > 1)
{
ChannelConfigDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = CorrectSlotSelections[e.RowIndex];
}
// If only one was found update the correct slot selections.
else if(rowsWithDuplicateValue.Count() == 1)
{
CorrectSlotSelections[e.RowIndex] = (int)cb.Value;
}
ChannelConfigDataGridView.Invalidate();
}
}