将数据行添加到数据表的预定义索引处 [英] Add data row to datatable at predefined index

查看:91
本文介绍了将数据行添加到数据表的预定义索引处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有一列的数据表:

I have a datatable with one column:

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

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

I then want to add a row to that datatable, but want to give a specific index, the commented number is the desired index:

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

更新:


  • 必须能够处理具有唯一
    索引的数百行输入。

  • 无论表中是否有足够的
    行用于insertat函数,索引都必须是我定义的索引。

推荐答案

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

If the value specified for the pos parameter is greater than the number of rows in the collection, the new row is added to the end.

这篇关于将数据行添加到数据表的预定义索引处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆