How to add New Column with Value to the Existing DataTable?

thevan picture thevan · Jun 21, 2011 · Viewed 234.9k times · Source

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

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

How to do this?

Answer

Keith Walton picture Keith Walton · Jun 21, 2011

Without For loop:

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);