在指定索引处的数据表中添加数据行 [英] Add a datarow in datatable at specified index

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

问题描述

亲爱的朋友们,



我需要在指定索引的数据集数据表中添加数据行。怎么做?



我有一个数据集填充数据集中的数据,我必须在数据集中添加一个新行到这个数据表(基于一些条件)。



谢谢



Varun Sareen

Dear Friends,

I need to add a data row in a dataset data table at a specified index. How to do that?.

I have a datatable filled with data in a dataset and i have to add a new row to this datatable in dataset(based on the some condition).

Thanks

Varun Sareen

推荐答案

private void InsertRow()

{

object [] objrow = new object [] {将数据放入要插入的内容

与你有相同的架构

你现有的数据表};



例如:

object [] objrow = new object [] {32,Cipla,Ram,DateTime.Now};






///我现有的数据表名称是table



DataRow row = table。 NEWROW(); ///在数据表中创建新行



row.ItemArray = objrow; ///将数据复制到数据行对象



table.Rows.InsertAt(row,3); ///在数据表的第三个索引处插入行

}
private void InsertRow()
{
object[] objrow = new object[] {"Put your data what you want to insert
with same schema which you have for
your existing data table"};

for example:
object[] objrow = new object[] { "32", "Cipla", "Ram", DateTime.Now };



///My existing datatable name is "table"

DataRow row = table.NewRow(); ///creating new row in your datatable

row.ItemArray = objrow;///copying your data into data row object

table.Rows.InsertAt(row, 3);///insert row at 3rd index of datatable
}


//Assuming this is the existing datatable you have
DataTable existingDataTable = GetMeDataFromSomeWhere();

//Add a new row to table
DataRow newRow = existingDataTable.NewRow();

newRow["ID"] = 999;
newRow["SomeColumn"] = "Manas Bhardwaj";

existingDataTable.Rows.Add(newRow);





DataTable没有提供任何接口来插入排在某个位置。但这可以在一个函数中完成,你可以在这个函数中创建一个 dataview [< a href =http://msdn.microsoft.com/en-us/library/system.data.dataview.aspxtarget =_ blanktitle =New Window> ^ ]并对结果进行排序根据你的意愿。



There DataTable does not provide any interface to insert a row at certain position. But that can be done within a function where you can create a dataview [^]and sort the results as per your wish.


玛纳斯的基本方法有效。



如果你想保留任何关系数据的位置(或者DataTable,或者通过SQL),你必须记住表基本上是免费的。为了获得您想要的效果,项目可以具有特定位置,我倾向于通过添加可以与ORDER BY子句一起使用以获得排序结果的额外字段(索引或位置)来工作。在这种情况下,插入已排序的数据相当于添加一行,该行的额外字段的值介于上下记录的值之间。



对于DataTable对象,其中一个Select方法允许对表的内容进行排序。以下URL MSDN文章 [ ^ ]显示了如何执行此操作 - 只需将过滤条件指定为TRUE即可不过滤数据。 br />


希望这有助于
Manas' basic approach works.

If you want to retain position in any relational data (either DataTable, or throught SQL), you must remember that tables are essentially order free. To get the effect you want, where items can have specific positions, I tend to work by adding an extra field (Index or Position) that can be used with an ORDER BY clause to get sorted results. In that case, inserting to the sorted data is equivalent to adding a row whose value for this extra field is between the values for the records above and below.

For DataTable objects, one of the Select method allows sorting the contents of the table. The following URL MSDN Article[^] shows how to do this - simply specify the filter condition as "TRUE" to not filter data.

Hope this helps


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

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