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

时间:2013-05-29 08:14:21

标签: winforms bindingsource

我正在使用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.

你能帮我吗?提前谢谢

3 个答案:

答案 0 :(得分:1)

尝试这种方法:

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

确保YourBindingSource.AllowNew = true;

或者从BindingSource的属性窗口中:

enter image description here

对我有用。