Add columns to a datatable in c#?

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

dt.Rows.Add(dr); shows an error saying This row already belongs to this table. ....

alt text


ANSWERS:


Use dt.Rows.Import, or dt.ImportRows, I forgot the name

or judging from your code, why you need the dt.Rows.Add? shouldn't that be omitted?


Why do you add the datarow again into the table?

Remove the line dt.Rows.Add(dr);

and edit the datatable directly like this dt.Rows[i]["IsDeleted"] = Convert.ToByte(0); using a for loop



 MORE:


 ? ASP NET - C#: Access controls by Id
 ? Add a bunch of controls dynamically to screen in iOS
 ? Can't dynamically add new literal to ASP.NET Panel Control
 ? Add dynamic ASP.NET controls via a DropDownList to the page
 ? WinForm: control won't add to a panel
 ? ASP.NET refresh Update Panel
 ? Create textbox dynamically without postback
 ? Second button Event is not happening in Update panel in aspx page
 ? ASP.Net: click event for dynamic button in dynamically created nested update panel is not raised
 ? button click event on update panel not firing in multiple user control