我有一张只有一列的数据表:
this.callsTable.Columns.Add("Call", typeof(String));
我想要向数据表格中添加一行,但是希望在指定的位置插入,注释中的数字即为期望的索引:
this.callsTable.Rows.Add("Legs"); //11
更新:
- 必须能够处理输入有数百行具有唯一索引的数据。
- 无论表中是否有足够的行数供插入函数使用,索引必须是我定义的。
我有一张只有一列的数据表:
this.callsTable.Columns.Add("Call", typeof(String));
我想要向数据表格中添加一行,但是希望在指定的位置插入,注释中的数字即为期望的索引:
this.callsTable.Rows.Add("Legs"); //11
更新:
您可以使用DataTable.Rows.InsertAt
方法。
DataRow dr = callsTable.NewRow(); //Create New Row
dr["Call"] = "Legs"; // Set Column Value
callsTable.Rows.InsertAt(dr, 11); // InsertAt specified position
参见:DataRowCollection.InsertAt 方法
如果 pos 参数指定的值大于集合中的行数,则新行将被添加到末尾。
InsertAt
只接受DataRow
作为参数,所以你必须使用它。但是你可以在迭代中重复使用变量dr
。 - Habibdr = dt.NewRow();
。 - Habib