如何将具有值的新列添加到现有数据表? [英] How to add New Column with Value to the Existing DataTable?

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

问题描述

我有一个 5 列 10 行的数据表.现在我想向 DataTable 添加一个新列,并且我想将 DropDownList 值分配给新列.因此,应该将 DropDownList 值添加到新列中 10 次.这个怎么做?注意:不使用 FOR LOOP.

I have One DataTable with 5 Columns and 10 Rows. Now I want to add one New Column to the DataTable and I want to assign DropDownList value to the New Column. So the DropDownList value should be added 10 times to the New Column. How to do this? Note: Without using FOR LOOP.

例如:我现有的数据表是这样的.

For Example: My Existing DataTable is like this.

   ID             Value
  -----          -------
    1              100
    2              150

现在我想向这个数据表添加一个新列CourseID".我有一个 DropDownList.其选定值为 1.所以我现有的表应该如下所示:

Now I want to add one New Column "CourseID" to this DataTable. I have One DropDownList. Its selected value is 1. So My Existing Table should be like below:

    ID              Value         CourseID
   -----            ------       ----------
    1                100             1
    2                150             1

如何做到这一点?

推荐答案

无 For 循环:

Dim newColumn As New Data.DataColumn("Foo", GetType(System.String))     
newColumn.DefaultValue = "Your DropDownList value" 
table.Columns.Add(newColumn) 

C#:

System.Data.DataColumn newColumn = new System.Data.DataColumn("Foo", typeof(System.String));
newColumn.DefaultValue = "Your DropDownList value";
table.Columns.Add(newColumn);

这篇关于如何将具有值的新列添加到现有数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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