0

I am using bindingsource . The problem is that when i do AddNew() in binding source then it give me the exception Item cannot be added to a read-only or fixed-size list. This form is Dialog. For your review i am adding the code

Main Form Code

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)
    {
    }
}

This is the Dialog Form Code

public frmIncomeAddList( )
{
    InitializeComponent();  

    FillCurrency();

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

    this.earningBindingSource.AddNew();

    this.earningBindingNavigatorSaveItem.Enabled = true;

    FillDropdown(); 
}

on Dialog Form this.earningBindingSource.AddNew(); i am getting exception Item cannot be added to a read-only or fixed-size list.

Can you please help me. Thank's in advance

Furkan Ekinci
  • 2,472
  • 3
  • 29
  • 39
A.Goutam
  • 3,422
  • 9
  • 42
  • 90
  • It's obvious that your BindingSource is bound to fixed size or read-only list. You should change the underlying datasource to make it editable or variable. – King King May 29 '13 at 15:08
  • but i am doing `this.earningBindingSource.AddNew();` here i am doing for `.AddNew()` – A.Goutam May 30 '13 at 06:27
  • I supposed your earningBindingSource is typeof BindingSource and AddNew() will do some adding new row to the underlying data source. – King King May 30 '13 at 09:25
  • @KingKing Then what you suggest ? – A.Goutam May 30 '13 at 10:11
  • Change your underlying datasource, if you declare it as readonly it can't be changed anyway. I don't know exactly what your datasource is so it's better to update your question to help others see what your datasource is. I usually create my datasource as DataTable. What about yours? – King King May 30 '13 at 11:32

3 Answers3

1

Try this approach:

List<Earning> earnings = ((IEnumerable<Earning>)earningBindingSource.DataSource).ToList();
earnings.Add(new Earning());
earningBindingSource.DataSource = earnings.AsEnumerable();
Hargoth
  • 11
  • 4
1

when parent table has no record, and add new row in child table then show this error.

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

Robert
  • 5,278
  • 43
  • 65
  • 115
1

Make sure that YourBindingSource.AllowNew = true;

Or from properties window of the BindingSource:

enter image description here

It worked for me..

Ahmed Suror
  • 439
  • 6
  • 17