Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 396
thevan Avatar asked Jun 21 '11 14:06

thevan


People also ask

How do I add a column to a DataTable?

You create DataColumn objects within a table by using the DataColumn constructor, or by calling the Add method of the Columns property of the table, which is a DataColumnCollection. The Add method accepts optional ColumnName, DataType, and Expression arguments and creates a new DataColumn as a member of the collection.


1 Answers

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); 
like image 96
Keith Walton Avatar answered Sep 22 '22 14:09

Keith Walton