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'。 –