9

我有一个包含一列的数据表:

this.callsTable.Columns.Add("Call", typeof(String));

然后我想向该数据表添加一行,但想给出一个特定的索引,注释的数字是所需的索引:

this.callsTable.Rows.Add("Legs"); //11

更新:

  • 必须能够处理输入数百行具有唯一索引的行。
  • 无论表中是否有足够的行用于 insertat 函数,索引都必须是我定义的。
4

1 回答 1

23

你可以使用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 参数指定的值大于集合中的行数,则将新行添加到末尾。

于 2013-12-17T14:15:07.457 回答