2
为什么我得到一个“有位置0处没有任何行”例外:将数据添加到类型化数据集编程
DSGeneral dg = new DSGeneral();
// I create 1 table
DSGeneral.GeneralDataTable t = new DSGeneral.GeneralDataTable();
dg.Tables.Add(t);
// The 1st row
DSGeneral.GeneralRow r = t.NewGeneralRow();
r.Code = "code";
r.Value = "7";
t.Rows.Add(r);
// The 2nd row
DSGeneral.GeneralRow r2 = t.NewGeneralRow();
r2.Code = "exp";
r2.Value = "5";
t.Rows.Add(r2);
它抛出这里:
int numClave = Convert.ToInt16(dg.General[0].Value);
此外,调试,我可以看到我的键入数据集dg
有2个表,我想知道为什么。
我编辑的代码是不带'添加()'两个表和行,但现在它说有一个没有行,当我做'DG .General.Rows.Count' – anmarti
@a_maar:是的,当然你还需要将行添加到'DataTable'中。只有表格存在,而不是行。 –
@a_maar:编辑我的答案以澄清这一点。必须有一个自动生成的方法'AddGeneralRow',您可以使用它来添加强类型的'DataRow'。 –