How can I add a new column and data to a datatable that already contains data?
Asked Answered
U

6

93

How do I add a new DataColumn to a DataTable object that already contains data?

PseudoCode

//call SQL helper class to get initial data 
DataTable dt = sql.ExecuteDataTable("sp_MyProc");

dt.Columns.Add("NewColumn", type(System.Int32));

foreach(DataRow row in dr.Rows)
{
    //need to set value to NewColumn column
}
Ufa answered 22/2, 2010 at 18:3 Comment(0)
S
157

Just keep going with your code - you're on the right track:

//call SQL helper class to get initial data 
DataTable dt = sql.ExecuteDataTable("sp_MyProc");

dt.Columns.Add("NewColumn", typeof(System.Int32));

foreach(DataRow row in dt.Rows)
{
    //need to set value to NewColumn column
    row["NewColumn"] = 0;   // or set it to some other value
}

// possibly save your Dataset here, after setting all the new values
Stillas answered 22/2, 2010 at 18:25 Comment(2)
Did you need to call dt.AcceptChanges() after I add the new column and value?Ufa
@Michael: no, not really. Your data set is now prepped with those new values. If you want to save it, you need to have some kind of method to write it back (through the SqlDataAdapter or some other means). AcceptChanges() won't do anything (except mark those changes as "accepted" -> they won't be detected for saving the next time you save your data!)Stillas
V
14

Should it not be foreach instead of for!?

//call SQL helper class to get initial data  
DataTable dt = sql.ExecuteDataTable("sp_MyProc"); 

dt.Columns.Add("MyRow", **typeof**(System.Int32)); 

foreach(DataRow dr in dt.Rows) 
{ 
    //need to set value to MyRow column 
    dr["MyRow"] = 0;   // or set it to some other value 
} 
Vergievergil answered 6/1, 2012 at 20:54 Comment(0)
G
10

Only you want to set default value parameter. This calling third overloading method.

dt.Columns.Add("MyRow", type(System.Int32),0);
Glutamate answered 18/2, 2015 at 10:51 Comment(1)
should "type" be "typeof" ?Kurtzig
F
9

Here is an alternate solution to reduce For/ForEach looping, this would reduce looping time and updates quickly :)

 dt.Columns.Add("MyRow", typeof(System.Int32));
 dt.Columns["MyRow"].Expression = "'0'";
Forcefeed answered 2/3, 2015 at 22:16 Comment(3)
@Akxaya, would you mind elaborating on how that solution works?Piperidine
DataTable dt = sql.ExecuteDataTable("sp_MyProc"); // dt.Columns.Add("MyRow", typeof(System.Int32)); dt.Columns["MyRow"].Expression = "'0'";Forcefeed
Expression supports a lot of other things like calculations, aggregates, comparisons, IIF, LIKE, ISNULL, etc.Telefilm
T
2

Try this

> dt.columns.Add("ColumnName", typeof(Give the type you want));
> dt.Rows[give the row no like  or  or any no]["Column name in which you want to add data"] = Value;
Trinette answered 14/7, 2015 at 18:3 Comment(0)
S
1

In net6.0 Language Version C# 10 the solution for setting a default value is:

dt.Columns.Add("MyRow", type(System.Int32)).DefaultValue = 0;
Snowclad answered 26/6, 2023 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.