0

我正在使用 bindingsource 。问题是,当我AddNew()在绑定源中执行此操作时,它给了我异常Item cannot be added to a read-only or fixed-size list.此表单是对话框。为了您的审查,我正在添加代码

主窗体代码

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.

你能帮我么。提前致谢

4

3 回答 3

1

试试这个方法:

List<Earning> earnings = ((IEnumerable<Earning>)earningBindingSource.DataSource).ToList();
earnings.Add(new Earning());
earningBindingSource.DataSource = earnings.AsEnumerable();
于 2015-02-03T12:01:28.283 回答
1

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

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

于 2016-01-22T08:55:48.370 回答
1

确保YourBindingSource.AllowNew = true;

或者从 BindingSource 的属性窗口:

在此处输入图像描述

它对我有用..

于 2019-02-23T23:59:12.887 回答