Sunday, August 10, 2008

Merging New Rows to an Existing DataSet

We can merge new records to an existing fillled dataset. Following is the example code:

//Existing Dataset
DataSet ds = code to fill dataset;

DataTable dt = reports.Tables[0];
foreach (DataRow dr in dt.Rows)
{
DataRow newRow = ds.Tables[0].NewRow();
newRow["name"] = dr["DisplayName"];
newRow["path"] = dr["ID"];
newRow["rolename"] = dr["Roles"];
newRow["username"] = HttpContext.Current.User.Identity.Name.ToString();


//Appending newly generated row to existing table in the dataset
ds.Tables[0].Rows.Add(newRow);
}

No comments: