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