Add columns to a datatable in c#?
Posted
by Pandiya Chendur
on Stack Overflow
See other posts from Stack Overflow
or by Pandiya Chendur
Published on 2010-05-11T05:54:57Z
Indexed on
2010/05/11
6:04 UTC
Read the original article
Hit count: 437
I have a csv reader class that reads a .csv
file and its values.... I have created datatable
out of it... Consider my Datatable
contains three header columns Name,EmailId,PhoneNo
.... The values have been added successfully.... Now i want to add two columns IsDeleted,CreatedDate
to this datatable
... I have tried this but it doesn't seem to work,
foreach (string strHeader in headers)
{
dt.Columns.Add(strHeader);
}
string[] data;
while ((data = reader.GetCSVLine()) != null)
{
dt.Rows.Add(data);
}
dt.Columns.Add("IsDeleted", typeof(byte));
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
foreach (DataRow dr in dt.Rows)
{
dr["IsDeleted"] = Convert.ToByte(0);
dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
dt.Rows.Add(dr);
}
When i try to add isdeleted
values an error saying This row already belongs to this table.
....
© Stack Overflow or respective owner