Hi,
I am creating a DataTable object which i am populating it with data from database and binding it to gridview control... I want to update,delete rows in this DataTable object without affecting the Datain my sql table and also bind this DataTable object to Gridview.. How to do this?
How to Add,Modify,Delete Rows in DataTable which has been populated with Data From Database table?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Krishna GaradRank #191 · 12.8K reputationPosted Jan 11, 2012, 12:46 AM EST
Datta KharadRank #2,135 · 668 reputationPosted Jan 11, 2012, 12:55 AM EST
//Adding rows into Datatable:-
DataRow row = null;
for (int i = 0; i < 5; i++)
{
row = dTable.NewRow();
row["AutoID"] = i + 1;
row["Name"] = i + " - Datta";
row["Address"] = "Mumbai, India - " + i;
dTable.Rows.Add(row);
}
//Modify certain values into the DataTable:-
dTable.Rows[2]["AutoID"] = 20;
dTable.Rows[2]["Name"] = "Modified";
dTable.Rows[2]["Address"] = "Modified Address";
dTable.AcceptChanges();
// Delete row:-
dTable.Rows[1].Delete();
dTable.AcceptChanges();
//OR
foreach( DataRow row in someTable.Rows )
{
if( /* your condition here */ )
row.Delete();
}
someTable.AcceptChanges(); //You then need to call AcceptChanges() on the data table to finalize the delete - presumably
//Filtering data from DataTable:-
DataRow[] rows1 = dTable.Select(" AutoID > 5", "AuotID ASC");
//For more detail of Datatable operation refer this link:-
http://www.dotnetfunda.com/articles/article131.aspx