项目无法添加到只读或固定大小的列表中

问题描述 投票:0回答:3

我正在使用bindingsource。问题是当我在绑定源中执行AddNew()然后它给我异常Item cannot be added to a read-only or fixed-size list.这个形式是Dialog。为了您的审查,我正在添加代码

主表格代码

private void bindingNavigatorAddNewItem_Click_1(object sender, EventArgs e)
{
    try
    {
        this.Validate();
        _earning = (Earning)this.earningBindingSource.Current;
        string EmpNo = Convert.ToString(_earning.Empno == null || _earning.Empno == string.Empty ? "0" : _earning.Empno);
        Incomes.frmIncomeAddList _earnEmployee = new Incomes.frmIncomeAddList();
        _earnEmployee.ShowDialog();
    }
    catch (Exception ex)
    {
    }
}

这是对话表格代码

public frmIncomeAddList( )
{
    InitializeComponent();  

    FillCurrency();

    FillDropdown();
    FillEarnCode();
    FillEarnCodeDESC();

    this.earningBindingSource.AddNew();

    this.earningBindingNavigatorSaveItem.Enabled = true;

    FillDropdown(); 
}

在对话形式this.earningBindingSource.AddNew();我得到例外Item cannot be added to a read-only or fixed-size list.

你能帮我么。提前致谢

winforms bindingsource
3个回答
1
投票

试试这种方法:

List<Earning> earnings = ((IEnumerable<Earning>)earningBindingSource.DataSource).ToList();
earnings.Add(new Earning());
earningBindingSource.DataSource = earnings.AsEnumerable();

1
投票

当父表没有记录,并在子表中添加新行,然后显示此错误。

Item cannot be added to a read-only or fixed-size list


0
投票

确保YourBindingSource.AllowNew = true;

或者从BindingSource的属性窗口:

enter image description here

它对我有用..

© www.soinside.com 2019 - 2024. All rights reserved.