从BindingList中删除项目不会刷新DataGridViewComboBoxCell

本文关键字:刷新 DataGridViewComboBoxCell 删除项目 BindingList | 更新日期: 2023-09-27 18:19:16

我有一个DataGridView,其中有一个单元格是DataGridViewComboCell。每个DataGridViewComboCell都绑定到一个BindingList的唯一副本。当我从绑定列表中删除一个项目时,组合框会删除我从绑定列表中删除的条目。
但是,如果选择了该值,则它将保持为单元格中的选定项。

我试着做一个datagridview.refresh(),但它仍然没有帮助。它从工具条菜单项

中被调用
  // _contractLists is List<BindingList<String>> which is the datasource for a datagridviewcombobox
List<String> removedList = new List<string>();
_contractSelForm.ShowDialog();
_contractSelForm.GetandClearRemovedContracts(ref removedList);
foreach (BindingList<String> contractList in _contractLists)
{
     // remove deleted favorites
    foreach (string contract_name in removedList)
    {
         contractList.Remove(contract_name);
    }
}  
dataGridView1.Refresh();
dataGridView1.EndEdit();          

从BindingList中删除项目不会刷新DataGridViewComboBoxCell

有几点需要注意:

1)你不需要在刷新后调用EndEdit。如果需要调用它,应该在Refresh之前调用它。

2)如果你的组合框有一个DropDownStyle的DropDown,那么这是预期的行为。

来自MSDN文档:

如果将DropDownStyle属性设置为DropDown,则可以在组合框的可编辑区域键入任何值。

要改变这一点,要么将DropDownStyle更改为DropDownList,要么在删除项后手动清除代码中的值。