【问题标题】:Removing item from BindingList does not refresh DataGridViewComboBoxCell从 BindingList 中删除项目不会刷新 DataGridViewComboBoxCell
【发布时间】:2012-01-03 22:31:58
【问题描述】:

我有一个 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();          

【问题讨论】:

  • 您是否正在移动和/或更改正确事件中的绑定..?你能粘贴你正在使用的代码吗..?也 datagridview.Bind() 或绑定听起来像是要看的东西..只是在这里猜测而没有看到您的代码
  • 您能说明如何将 BindingLists 绑定到组合框列吗? dataGridView1 是否处于虚拟模式?

标签: c# winforms datagridviewcomboboxcell


【解决方案1】:

需要注意/看的几件事:

1) 您不需要在刷新后调用 EndEdit。如果需要调用,请在 Refresh 之前调用。

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

来自MSDN documentation

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

要更改此设置,请将 DropDownStyle 更改为 DropDownList 或在删除项目后手动清除代码中的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-18
    • 2014-06-17
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多