Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a DataTable with one column filled with list of words from a text file, I create a method to read a string if the string is founded the row must be deleted, but the problem is that the DataTable don't get the updates.

C#
foreach(string line in file)
{
   tagst.Rows.Add(line)
}

string s;
for (int k = 0; k < tagst.Rows.Count; k++)
{
   s = tagst.Rows[k]["Tags"].ToString();
   if(s.Equals("Jad"))
   {
      tagst.Rows[k].Delete();
   }
}
Posted
Comments
ZurdoDev 30-Apr-14 15:50pm    
Where are you stuck?
Member 10672813 30-Apr-14 17:12pm    
The DataTable (tagst) is filled with data, but after I update it (as in the for loop), If I display the result it will not be changed.
I tried to use submitchanges method but it is not working.
[no name] 30-Apr-14 16:46pm    
You probably need to refresh your page.
syed shanu 1-May-14 0:36am    
Try This :
for (int k = 0; k < tagst.Rows.Count; k++)
{
s = tagst.Rows[k]["Tags"].ToString();
if(s.Equals("Jad"))
{
tagst.Rows[k].Delete();
}
}
tagst.AcceptChanges();

1 solution

Delete does not remove the row from the DataTable. It tags the row to be deleted.

If you call AcceptChanges on the DataTable or the DataRow you changed, THEN the row will be removed from the DataTable.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900