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!

DataTable + delete selected Rows
basedcp
found my code...;)
clint 2
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;Koray Samsun
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!
EpicJohn
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
Tarkan G.
there is ....
theDataTable.Rows.Remove(object);
its in the data table you have made :-)
in your case:
Table.Rows.Remove();
Aleksey Nagoga.
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.
CSharpShooter
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
DTHMTLGOD
BradDaBug
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!
Jakein2006
AndyK
Hi Ahmed
I dont see any method with name "Remove". Let me know.
Thanks!
WayneSpangler