locked
DataTable + delete selected Rows RRS feed

  • Question

  • Hi

     

     

    I have a data table , which gets populated at the end of a certain transaction with say 30 Rows. Now depending upon the condition the user selects, i wanted to delete certain rows as given below.But the thing here happening is, first time the fifth row gets deleted,the row count is getting changed in the table to 29, which is playing the trick here as we can see that when next delete statement isencountered, there is no row with index 29 which is throwing an error and some times the row which i want to delete is not getting deleted, instead another row with altered index is getting deleted.How can we have a workaround for this situation? My intention is just to delete the rows at the specified index from the original output table.

    if (Condition)

    {

    Table.Rows[5].Delete();

    Table.Rows[29].Delete();

    Table.Rows[12].Delete();

    Table.Rows[13].Delete();

    Table.Rows[19].Delete();

    Table.Rows[20].Delete();

    Table.Rows[26].Delete();

    Table.Rows[27].Delete();

    }

     

    Thanks!

     

    Thursday, August 17, 2006 11:43 AM

Answers

  • you would be better to use the Remove() method and give it the dataRow to remove.

     

    example:

     

    DataRow theRowToDelete = theDataTable.Rows[Index];

    theDataTable.Row.Remove(theRowToDelete);

     

    I think the RemoveAt() would probably encounter the same problem as you are currently having.

     

    does this help?

    Thursday, August 17, 2006 1:15 PM

All replies

  • you would be better to use the Remove() method and give it the dataRow to remove.

     

    example:

     

    DataRow theRowToDelete = theDataTable.Rows[Index];

    theDataTable.Row.Remove(theRowToDelete);

     

    I think the RemoveAt() would probably encounter the same problem as you are currently having.

     

    does this help?

    Thursday, August 17, 2006 1:15 PM
  • Hi Ahmed

    I dont see any method with name "Remove". Let me know.

     

    Thanks!

    Thursday, August 17, 2006 2:08 PM
  • there is ....

     

    theDataTable.Rows.Remove(object);

     

    its in the data table you have made :-)

     

    in your case:

     

    Table.Rows.Remove();

    Thursday, August 17, 2006 2:14 PM
  • Hi Ahmed

    Sorry, This method is there. But this logic is not working. Its giving me the same error. Suppose if i removed a row at index 5, then the table rows count is now becoming 30 as compared to original 31. So, when i go to remove a row at index 30 as from original table, the error says that there is no row available at the original index. Hope iam clear.

     

    Thanks!

     

    Thursday, August 17, 2006 2:34 PM
  • ok, so you are wanting to remove specific rows of data in the dataTable correct? Or are these values being deleted from a datagridview? I will in the mean time try to find another solution

     

    re-reading your post, yes of course that will be bad because as you have experienced yourself, the referenced row index is non existant as when you delete a row, the index changes.

     

    Question I have is, which I just need to know really, why do you wish to remove certain rows of data?

     

     

    Thursday, August 17, 2006 7:04 PM
  • not the perfect solution, but couldn't you mark the records then remove all records with you mark? bit dirty, but it's late and i get like that ;)
    Thursday, August 17, 2006 8:31 PM
  • well yes you could do that, so when you select a record to mark as "dirty", select the record, press a "mark as dirty" button, it will then store the values in an array for example, of which records to delete (basically getting the DataRow of the record chosen to delete), once done, simply go through each item chosen as "dirty" of the DataRow's stored in a collection then do the DataTable.Rows.Remove(DataRow) method I had described earlier.
    Thursday, August 17, 2006 8:41 PM
  • actually, i was just monkeying around to confirm my suspicions.

    if you already know the rows you are going to delete you can delete by:

    table.rows.delete(datarow)

    or

    simply datarow.delete

     

    as they are all objects, they have the same effect. 

     

                DataTable dtTest = new DataTable();
    
                dtTest.Columns.Add(new DataColumn("col1", typeof(string)));
                dtTest.Columns.Add(new DataColumn("col2", typeof(string)));
    
                dtTest.Rows.Add("first line");
                dtTest.Rows.Add("second line");
                dtTest.Rows.Add("third line");
                dtTest.Rows.Add("fourth line");
    
                DataRow dr1 = dtTest.Rows[0];
                DataRow dr2 = dtTest.Rows[2];
    
                //method one
                dtTest.Rows.Remove(dr2);
                dtTest.Rows.Remove(dr1);
    
                //method two
                dr1.Delete();
                dr2.Delete();
    
                dataGridView1.DataSource = dtTest;
    If you delete the row by using the table index, then you will get messed up as the index keeps changing as you delete rows. 
    this way you can delete the object you want to delete as many times as you like with no exceptions :) 
    Thursday, August 17, 2006 8:47 PM
  • i don't know whats gfoing on here, i thought i just posted a working example of this, i'll bet i've posted it to another question.....anyway, all i was going to say was that ahmedilyas  was right.  The remove DOES work as it removes the object freom the table. 

    As, you already know the rows you want to delete you can actually do datarow.delete that will remove it from the table for you.  You can use both methods as many times as you like.  .NET seems to look after you stuff for you. 

    Thursday, August 17, 2006 8:53 PM
  • found my code...;)

    Thursday, August 17, 2006 8:54 PM
  • hehe - glad I could help ;-)
    Thursday, August 17, 2006 9:34 PM
  • Hi

     

    Well, i have certain condition the user selects, based on the condition, the user will see only certain rows out of my output table, he will not see all the rows and as the condition varies the rows from output table varies. Basically i have certain logic written which will pull all combinations of data in to the final output table. Now, i have certain conditions left to the users choice, depending upon which i have to associate only certain rows from this outtable to the grid. And again there after user has the option to save this data to the backend once he feel that he is ok with the data. So the data is getting saved ultimately only after selecting certain rows from the output table depending upon the condition.

     

    Now the output table is created in in C#, the rows are not flowing anything from backend.This table contain rows from other intermediate tables which are pulling data from the backend.

     

    Can you suggest any logic for this?

     

    Thanks!

     

    Friday, August 18, 2006 7:31 AM
  •  

    I found another way of doing it without marking them as "dirty".. Here is an example :)

             int i= 0;
             while(i < ds_xml.Tables[xmltable].Rows.Count)
             {
                DataRow dr = ds_xml.Tables[xmltable].Rows[i];
                if(dr[0].ToString() != Ops.ToString())
                {
                   ds_xml.Tables[xmltable].Rows.Remove(dr);
                }
                else
                {
                   i++;
                }
             }

     

    Hope it hels somebody :)

    • Proposed as answer by Luis Bragado Saturday, December 4, 2010 1:21 AM
    Thursday, August 5, 2010 1:09 PM
  • Thanks Veay, that works!!!
    Saturday, December 4, 2010 1:22 AM
  • Delete the higher index first.
    Sunday, December 5, 2010 12:16 AM
  • To paraphrase Louis, iterate through the Rows backwards:

    DataRow dr;
    for (i = ds_xml.Tables[xmltable].Rows.Count -1; i >= 0; i--)
    {
      dr = ds_xml.Tables[xmltable].Rows[i];
      if(dr[0].ToString() != Ops.ToString())
        ds_xml.Tables[xmltable].Rows.Remove(dr);
    }
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by aagrawal Monday, November 5, 2012 11:00 AM
    • Unproposed as answer by aagrawal Monday, November 5, 2012 11:01 AM
    Sunday, December 5, 2010 12:28 AM