Insert ,Update and Delete in DataTable using c#
Creating DataTable
DataTable dt= new DataTable();
DataColumn columnAutoID = new DataColumn("AutoID", typeof(int));
columnAutoID.AutoIncrement = true;
columnAutoID.AutoIncrementSeed = 1;
dt.Columns.Add(columnAutoID);
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
dt.Columns.Add(new DataColumn("Phone", typeof(int)));
dt.Columns.Add(new DataColumn("UpdateStatus", typeof(int)));
Adding New row witrh data in DataTable
DataRow row = dt.NewRow();
row["Name "] = "kandy";
row["Address "] = "India";
row["Phone "] = "888888888";
row["UpdateStatus "] = "0";
dt.Rows.Add(row);
Updating DataTable when UpdateStatus=0
DataRow[] rows = dt.Select("UpdateStatus =" + 0);
if (rows.Length > 0)
{
foreach (DataRow row in rows)
{
row["Name "] = “kandySam”;
row["Address "] = “India”;
row["Phone "] = “888888888”;
row["UpdateStatus "] = “0”;
dtNote.AcceptChanges();
row.SetModified();
}
}
Delete Row from DataTable.
DataRow[] row1 = dt.Select("AutoId =" + Convert.ToInt32(1));
foreach (var rows in row1)
rows.Delete();