简体   繁体   English

如何在数据集中的确切位置动态添加行

[英]How can i add the row dynamically at an exact postion in a dataset

I write a code to add dynamical column and row to a data set but the field is added some where in my data-set so can any one tell how can I resolve this 我编写了代码以将动态列和行添加到数据集中,但是将该字段添加到了数据集中的某些位置,所以任何人都可以告诉我如何解决此问题

My code 我的代码

for (int i = 0; i < AchDB.Amount1.Count; i++)
    {
        DataColumn dc = new DataColumn("Amount1");
        local_ds.Tables[0].Columns.Add(dc);
        local_ds.Tables[0].Rows.Add(AchDB.Amount1[i]);
    }

I need as per in the image shown 我需要根据显示的图像

在此输入图像描述

Maybe you can use this: 也许你可以使用这个:

DataColumn dc = new DataColumn("Amount1");
local_ds.Tables[0].Columns.Add(dc);

for (int i = 0; i < AchDB.Amount1.Count; i++)
{
    local_ds.Tables[0].Rows[i]["Amount1"] = AchDB.Amount1[i];
}

I did put the code for adding a new column outside the loop. 我确实在循环外添加了用于添加新列的代码。 I think adding the column once will be applicable for all rows. 我认为添加该列一次将适用于所有行。

You can use the "InsertAt" method of DataTable for inserting the row at specific position. 您可以使用DataTable的“ InsertAt”方法在特定位置插入行。 For the columns there is no direct way of adding a column at a specific position. 对于列,没有直接的方法可以在特定位置添加列。 When you add a column to a datatable - it always gets added at the last. 当您将列添加到数据表时-它总是在最后添加。

dt.Rows.InsertAt(newRowObject,pos);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM