如何在数据表中任意位置插入行? [英] How to insert row at any desired position in datatable?
本文介绍了如何在数据表中任意位置插入行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据表,其中包含10行。我现在需要一些条件的指定位置插入11行。
我已经试过了InsertAt方法,但是,让该行已属于另一个表的错误。
我不能用ImportRow方法,简单地将行导入到数据表,然后将该行的现有行的末尾。
我应该怎么办?请帮助!
感谢
修订code
INT ICOUNT = 0;
的foreach(在dtWithBundle.Rows的DataRow博士)
{
DataRow的drClone = dtOppClone.NewRow();
drClone.ItemArray = dr.ItemArray;
dtOpps.Rows.InsertAt(drClone,iIndex + ICOUNT);
//dtOpps.ImportRow(drClone);
//dtOpps.Rows.Add(drClone.ItemArray); //评论对8 - 4 2011 1700HRS
ICOUNT ++;
dtOpps.AcceptChanges();
}
解决方案
试试这个。我觉得你得到的错误是BCZ没有创建NEWROW。
DataTable的DT =新的DataTable();
dt.Columns.Add(姓名,typeof运算(字符串));
DataRow的博士;
博士= dt.NewRow();
博士[0] =A;
dt.Rows.Add(DR);
博士= dt.NewRow();
博士[0] =C;
dt.Rows.Add(DR);
博士= dt.NewRow();
博士[0] =B;
dt.Rows.InsertAt(博士,1);
的foreach(DataRow的d在dt.Rows)
{
Console.WriteLine(D [0]的ToString());
}
Console.Read();
I have a datatable which contains 10 rows. I now need to insert 11th row at the position specified by some conditions.
I have tried the InsertAt method but that gives the error of "this row already belongs to another table".
I cannot use ImportRow method as that simply import the rows into datatable and inserts the row at the end of the existing rows.
What should i do? Kindly help!
Thanks
UPDATED CODE
int iCount = 0;
foreach (DataRow dr in dtWithBundle.Rows)
{
DataRow drClone = dtOppClone.NewRow();
drClone.ItemArray = dr.ItemArray;
dtOpps.Rows.InsertAt(drClone, iIndex + iCount);
//dtOpps.ImportRow(drClone);
//dtOpps.Rows.Add(drClone.ItemArray); // Commented on Aug-4 2011 1700HRS
iCount++;
dtOpps.AcceptChanges();
}
解决方案
Try this. I think the error you are getting is bcz you are not creating NewRow.
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
DataRow dr;
dr = dt.NewRow();
dr[0] = "A";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "C";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "B";
dt.Rows.InsertAt(dr,1);
foreach (DataRow d in dt.Rows)
{
Console.WriteLine(d[0].ToString());
}
Console.Read();
这篇关于如何在数据表中任意位置插入行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文