在现有数据表中添加按顺序编号的新列的最佳方法

发布于 2024-10-05 06:38:42 字数 310 浏览 8 评论 0原文

我有一个非空数据表。向其中添加从 1 开始按顺序编号的另一列的最佳方法是什么

。我尝试了以下代码。但没有成功。

DataColumn dc = new DataColumn("Col1");
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;       
dc.DataType = typeof(Int32);
dt.Columns.Add(dc);

在这种情况下设置任何表达式会有帮助吗?

提前致谢

I have a non empty datatable . What is the best way to add another column to it that has sequential numbering starting from 1.

I tried the following code. But did not work.

DataColumn dc = new DataColumn("Col1");
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;       
dc.DataType = typeof(Int32);
dt.Columns.Add(dc);

Will setting any expression help in this scenario ?

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安穩 2024-10-12 06:38:42

我认为您可以通过使用第二个“帮助器”数据表来实现这一点,该数据表仅包含一个自动增量字段,然后用现有数据填充/合并它,如下所示:

DataTable dtIncremented  = new DataTable(dt.TableName);
DataColumn dc            = new DataColumn("Col1");
dc.AutoIncrement         = true;
dc.AutoIncrementSeed     = 1;
dc.AutoIncrementStep     = 1;
dc.DataType              = typeof(Int32);    
dtIncremented.Columns.Add(dc);

dtIncremented.BeginLoadData();

DataTableReader dtReader = new DataTableReader(dt);
dtIncremented.Load(dtReader);

dtIncremented.EndLoadData();

然后您只需返回 dtIncremented 表而不是原来的 dt。这不是一个优雅的解决方案,但应该可行。

I think you could achieve that by using a 2nd "helper" data table that would contain just an auto-increment field and then you populate/merge it with the existing data, something like this:

DataTable dtIncremented  = new DataTable(dt.TableName);
DataColumn dc            = new DataColumn("Col1");
dc.AutoIncrement         = true;
dc.AutoIncrementSeed     = 1;
dc.AutoIncrementStep     = 1;
dc.DataType              = typeof(Int32);    
dtIncremented.Columns.Add(dc);

dtIncremented.BeginLoadData();

DataTableReader dtReader = new DataTableReader(dt);
dtIncremented.Load(dtReader);

dtIncremented.EndLoadData();

And then you would just return dtIncremented table instead of the original dt. Not an elegant solution but should work.

围归者 2024-10-12 06:38:42

下面的代码对我有用

代码已编辑

        // Added temp rows so that this solution can mimic actual requirement

        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("Col");
        dt.Columns.Add(dc1);

        for(int i=0;i<10;i++)
        {
            DataRow dr = dt.NewRow();
             dr["Col"] = i.ToString();
             dt.Rows.Add(dr);
        }


        // Added new column with Autoincrement, 

        DataColumn dc = new DataColumn("Col1"); 
        dc.AutoIncrement = true;
        dc.AutoIncrementSeed = 1;
        dc.DataType = typeof(Int32);

        // Handeled CollectionChanged event

        dt.Columns.CollectionChanged += new CollectionChangeEventHandler(Columns_CollectionChanged);
        dt.Columns.Add(dc);

        // After column added demostratation

        DataRow dr1 = dt.NewRow();
            dt.Rows.Add(dr1);



    void Columns_CollectionChanged(object sender, CollectionChangeEventArgs e)
    {
        DataColumn dc = (e.Element as DataColumn);
        if (dc != null && dc.AutoIncrement)
        {
            long i = dc.AutoIncrementSeed;
            foreach (DataRow drow in dc.Table.Rows)
            {
                drow[dc] = i;
                i++;
            }
        }
    }

below code worked for me

Code is Edited

        // Added temp rows so that this solution can mimic actual requirement

        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("Col");
        dt.Columns.Add(dc1);

        for(int i=0;i<10;i++)
        {
            DataRow dr = dt.NewRow();
             dr["Col"] = i.ToString();
             dt.Rows.Add(dr);
        }


        // Added new column with Autoincrement, 

        DataColumn dc = new DataColumn("Col1"); 
        dc.AutoIncrement = true;
        dc.AutoIncrementSeed = 1;
        dc.DataType = typeof(Int32);

        // Handeled CollectionChanged event

        dt.Columns.CollectionChanged += new CollectionChangeEventHandler(Columns_CollectionChanged);
        dt.Columns.Add(dc);

        // After column added demostratation

        DataRow dr1 = dt.NewRow();
            dt.Rows.Add(dr1);



    void Columns_CollectionChanged(object sender, CollectionChangeEventArgs e)
    {
        DataColumn dc = (e.Element as DataColumn);
        if (dc != null && dc.AutoIncrement)
        {
            long i = dc.AutoIncrementSeed;
            foreach (DataRow drow in dc.Table.Rows)
            {
                drow[dc] = i;
                i++;
            }
        }
    }
她说她爱他 2024-10-12 06:38:42

您必须为此构建一个全新的数据表,并手动将每一行从旧表逐一深度复制到新表。对不起。

You'll have to build a whole new datatable for this and manually deep-copy each row one by one from the old table to the new. Sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文