Reputation: 85
i have a datatable say DT having 11rows and i tried to delete rows having id=3 using
string DeptID ="3";
string s = "id='" + DeptID + "'";
rows = DT.Select(s);
foreach (DataRow r in rows)
r.Delete();
i am having 5 rows having id=3 but after deletion the number of rows in datatable not changed and some red color symbol comes in each fields(columns) of the deleted rows.Can anyone know why this is happening?
and whenever i tried to acces the remaining rows using
for(int i=0;i<dt1.Rows.Count;i++)
{
if (dt1.Rows[i][0].ToString() == "")
{
}
}
it shows error 'deleted rows cannot take'
Upvotes: 0
Views: 1260
Reputation: 1066
After deleting the rows,please give Acceptchanges() to the datatable,so that,it will get synchronized with new data.
dt.AcceptChanges();
//And u can also add a condition,while fetching the data,
foreach (DataRow dr in rows)
{ if(dr.RowState!=DataRowState.Deleted ||dr.RowState!=DataRowState.Detached)
{
}
}
Upvotes: 5
Reputation: 1941
You marked the row for deletion, that's why the red color symbols appear. row.Delete()
only changes your row state to Deleted. When you accept then changes (via AcceptChanges
method), the actual deletion will occur.
For more information, these are some msdn links: http://msdn.microsoft.com/en-us/library/03c7a3zb.aspx http://msdn.microsoft.com/en-us/library/system.data.datarow.delete.aspx http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx
Upvotes: 2