Data Table add new columns & values

  • Thread starter Thread starter Priya Bange
  • Start date Start date
P

Priya Bange

Guest
Hi Experts,

Am currently generating a data table with below approach. Once the data table gets loaded with data.

Am looking to add 2 more columns on top of the same data table and for those newly added columns populate them with same value for every existing record.

Please help with code block as new to C#. Am able to do step 1.


1. This step loads the data table

SqlDataReader readIn = go.ExecuteReader();
dataTable.Load(readIn);

if (dataTable == null || dataTable.Rows.Count == 0) return;

Let say sample result that got loaded into the data table

Column_1 , Column_2 , Column_3
txt_1 , Value_2 , Value_3
txt_2 , Value_xh , Value_3239
txt_3 , value_646 , value_5425



2. This step to add new columns to existing data table that got created

string column4 = "Hello"
string column 5 = "World"


dataTable.Columns.Add("column_4", typeof(string));
dataTable.Columns.Add("column_5", typeof(string));

Final Result Set


Column_1, Column_2, Column_3, Column_4, Column_5
txt_1 , Value_2, Value_3 , Hello , World
txt_2 , Value_xh, Value_3239,Hello, World
txt_3, value_646, value_5425, Hello, World


Thanks

Priya

Continue reading...
 
Back
Top