Click here to Skip to main content
16,020,305 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void btndelete_Click(object sender, RoutedEventArgs e)
        {
            int id=Convert.ToInt32(deptno_txt.Text);
            foreach (DataRow dr in ds.Tables[0].Rows)
           {
               if (id == 101)
               {
                    ds.Tables[0].Rows[0].Delete();
                }
              else if (id == 102)
               {
                    ds.Tables[0].Rows[1].Delete();
               }
                else
                    ds.Tables[0].Rows[2].Delete();
            }


I'm trying in this fashion. It's working but if I have 1000 records then it becomes difficult. So I want simple code that deletes records in dataset so that I can update to database using commandbuilder.
Posted
Updated 2-Sep-10 23:40pm
v3

this is not working.please make it proper.i'm able to delete only 1st row
 
Share this answer
 
Comments
T.Saravanann 7-Sep-10 9:00am    
Hey then why are you using for loop in your code.
Just using
ds.Tables[0].Row[0].Delete();

Don't write your comment in answer section.
Hi vikky423,

C#
private void btndelete_Click(object sender, RoutedEventArgs e)
        {
            for(int nRow=0;nRow<=ds.Tables[0].Rows.Count;nRow++)
            {
               if(ds.Tables[0].Rows[0]["Id"].ToString().Equals(txtbox.Text))
                {
                  ds.Tables[0].Rows[nRow].Delete();
                  ds.Tables[0].AcceptChanges();
                  --nRow;  // If Id comes multiple times in Dataset.
                  break;  // else use break it come out from loop.
                } 
           
            } 
        }

Here Id --> is your Column Name
<= --> use only less than not less than equal

Cheers :)
 
Share this answer
 

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



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