调用BindingSource.RemoveCurrent时如何更新基础列表

时间:2014-11-04 16:59:01

标签: c# winforms bindingsource

我有以下简单的类;

public class MyObject
{
    public int Id {get;set;}
    public string Name {get;set;}
}

List<MyObject> oList = new List<MyObject>();

我的列表中填充了一些项目。然后我使用像

这样的列表填充我的BindingSource
MyBindingSource.DataSource = oList; //contains some items in a list

My BindingSource链接到DataGridView(在这个例子中并不重要),但是根据DataGridView中的选定行,我的数据网格视图点击按钮有以下方法;

private void MyDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == btnRemove.Index)
    {
        MyBindingSource.RemoveCurrent();
    }
}

电话

  

MyBindingSource.RemoveCurrent()

从DataGridView中删除项目,但如何从oList的基础列表中删除该项目。

我认为分配 MyBindingSource.DataSource = oList,意味着 MyBindingSource.DataSource 中显示的列表实际指向oList?

1 个答案:

答案 0 :(得分:2)

List<T>不够聪明,知道事情已发生变化,因此请尝试使用System.ComponentModel中的BindingList<T>

BindingList<MyObject> oList = new BindingList<MyObject>();