简体   繁体   English

如何将New Column with Value添加到现有DataTable?

[英]How to add New Column with Value to the Existing DataTable?

I have One DataTable with 5 Columns and 10 Rows. 我有一个DataTable有5列和10行。 Now I want to add one New Column to the DataTable and I want to assign DropDownList value to the New Column. 现在我想向DataTable添加一个新列,我想将DropDownList值分配给New Column。 So the DropDownList value should be added 10 times to the New Column. 所以DropDownList值应该添加10次到New Column。 How to do this? 这该怎么做? Note: Without using FOR LOOP. 注意:不使用FOR LOOP。

For Example: My Existing DataTable is like this. 例如:我的现有DataTable是这样的。

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

Now I want to add one New Column "CourseID" to this DataTable. 现在我想在此DataTable中添加一个新列“CourseID”。 I have One DropDownList. 我有一个DropDownList。 Its selected value is 1. So My Existing Table should be like below: 它的选择值是1.所以我现有的表应如下所示:

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

How to do this? 这该怎么做?

Without For loop: 没有For循环:

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

C#: C#:

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

Add the column and update all rows in the DataTable , for example: 添加列并更新DataTable所有行,例如:

DataTable tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID", typeof(Int32)));
tbl.Columns.Add(new DataColumn("Name", typeof(string)));
for (Int32 i = 1; i <= 10; i++) {
    DataRow row = tbl.NewRow();
    row["ID"] = i;
    row["Name"] = i + ". row";
    tbl.Rows.Add(row);
}
DataColumn newCol = new DataColumn("NewColumn", typeof(string));
newCol.AllowDBNull = true;
tbl.Columns.Add(newCol);
foreach (DataRow row in tbl.Rows) {
    row["NewColumn"] = "You DropDownList value";
}
//if you don't want to allow null-values'
newCol.AllowDBNull = false;
//Data Table

 protected DataTable tblDynamic
        {
            get
            {
                return (DataTable)ViewState["tblDynamic"];
            }
            set
            {
                ViewState["tblDynamic"] = value;
            }
        }
//DynamicReport_GetUserType() function for getting data from DB


System.Data.DataSet ds = manage.DynamicReport_GetUserType();
                tblDynamic = ds.Tables[13];

//Add Column as "TypeName"

                tblDynamic.Columns.Add(new DataColumn("TypeName", typeof(string)));

//fill column data against ds.Tables[13]


                for (int i = 0; i < tblDynamic.Rows.Count; i++)
                {

                    if (tblDynamic.Rows[i]["Type"].ToString()=="A")
                    {
                        tblDynamic.Rows[i]["TypeName"] = "Apple";
                    }
                    if (tblDynamic.Rows[i]["Type"].ToString() == "B")
                    {
                        tblDynamic.Rows[i]["TypeName"] = "Ball";
                    }
                    if (tblDynamic.Rows[i]["Type"].ToString() == "C")
                    {
                        tblDynamic.Rows[i]["TypeName"] = "Cat";
                    }
                    if (tblDynamic.Rows[i]["Type"].ToString() == "D")
                    {
                        tblDynamic.Rows[i]["TypeName"] = "Dog;
                    }
                }

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

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