How to delete rows?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For the
life of me i can't figure out how

Option 1: foreach
foreach (DataRow row in dataSet.MY_TABLE.Rows) {
row.Delete();
}

This doesn't work. It erases the first row and then throws the exception
Someone Changed The Enumerator. No fun

Option 2: Clear()
dataSet.MY_TABLE.Clear();

This wipes out all the rows. Unfortunately, when i go to save the changes
the rows don't show up. Here's the update code:

foreach (dataSet.DataRow row in table.GetChanges().Rows)
{
switch (row.RowState)
{
case DataRowState.Deleted:
//--- Never get here

Apparently Clear() doesn't update whatever it is that leads to GetChanges
and RowState

So those are what i've tried and have had no luck with. Other ideas?

In experimenting, it looks like perhaps the proper way to do these things is
to use for i=0 to Rows.Count. That will leave the rows where they are but two
things will happen. First, the RowState will get set to Deleted. Second, any
attempt to read a deleted row will throw an exception. This bothers me
because it seems like calling Delete from for i= and from foreach results in
different behavior - if it's just marking rows it doesn't seem like calling
Delete should have screwed up the enumerator

i think i can hobble my code together but does anyone have any insight as to
how this works and why?

-baylor
 
Baylor,
Have you tried:

foreach (DataRow row in dataSet.MY_TABLE.Select()) {
row.Delete();
}

The DataTable.Select method will return an array of DataRows, which the
foreach will iterate over, instead of accessing the DataRowCollection
(DataTable.Rows property) directly.

Hope this helps
Jay


|i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For the
| life of me i can't figure out how
|
| Option 1: foreach
| foreach (DataRow row in dataSet.MY_TABLE.Rows) {
| row.Delete();
| }
|
| This doesn't work. It erases the first row and then throws the exception
| Someone Changed The Enumerator. No fun
|
| Option 2: Clear()
| dataSet.MY_TABLE.Clear();
|
| This wipes out all the rows. Unfortunately, when i go to save the changes
| the rows don't show up. Here's the update code:
|
| foreach (dataSet.DataRow row in table.GetChanges().Rows)
| {
| switch (row.RowState)
| {
| case DataRowState.Deleted:
| //--- Never get here
|
| Apparently Clear() doesn't update whatever it is that leads to GetChanges
| and RowState
|
| So those are what i've tried and have had no luck with. Other ideas?
|
| In experimenting, it looks like perhaps the proper way to do these things
is
| to use for i=0 to Rows.Count. That will leave the rows where they are but
two
| things will happen. First, the RowState will get set to Deleted. Second,
any
| attempt to read a deleted row will throw an exception. This bothers me
| because it seems like calling Delete from for i= and from foreach results
in
| different behavior - if it's just marking rows it doesn't seem like
calling
| Delete should have screwed up the enumerator
|
| i think i can hobble my code together but does anyone have any insight as
to
| how this works and why?
|
| -baylor
 
I think you have to capture the rowcount in a separate variable and then use
a for or while loop that uses the variable. Something like
table.rows.delete();
 
Baylor,

When I am deleting in Net from a collection as you do now, do I take forever
the approach to do it bottom up using the for index

Maybe you can do in future C# versions something with the yield for that,
however that is just a guess of my.

I hope this helps,

Cor
 
Richard,
Why? The sample I gave works for DataTables! As does the more general
approach described by Cor.

Hope this helps
Jay


|
| I think you have to capture the rowcount in a separate variable and then
use
| a for or while loop that uses the variable. Something like
| table.rows.delete();
|
|
| | > Baylor,
| > Have you tried:
| >
| > foreach (DataRow row in dataSet.MY_TABLE.Select()) {
| > row.Delete();
| > }
| >
| > The DataTable.Select method will return an array of DataRows, which the
| > foreach will iterate over, instead of accessing the DataRowCollection
| > (DataTable.Rows property) directly.
| >
| > Hope this helps
| > Jay
| >
| >
| > | > |i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For
| > the
| > | life of me i can't figure out how
| > |
| > | Option 1: foreach
| > | foreach (DataRow row in dataSet.MY_TABLE.Rows) {
| > | row.Delete();
| > | }
| > |
| > | This doesn't work. It erases the first row and then throws the
exception
| > | Someone Changed The Enumerator. No fun
| > |
| > | Option 2: Clear()
| > | dataSet.MY_TABLE.Clear();
| > |
| > | This wipes out all the rows. Unfortunately, when i go to save the
| > changes
| > | the rows don't show up. Here's the update code:
| > |
| > | foreach (dataSet.DataRow row in table.GetChanges().Rows)
| > | {
| > | switch (row.RowState)
| > | {
| > | case DataRowState.Deleted:
| > | //--- Never get here
| > |
| > | Apparently Clear() doesn't update whatever it is that leads to
| > GetChanges
| > | and RowState
| > |
| > | So those are what i've tried and have had no luck with. Other ideas?
| > |
| > | In experimenting, it looks like perhaps the proper way to do these
| > things
| > is
| > | to use for i=0 to Rows.Count. That will leave the rows where they are
| > but
| > two
| > | things will happen. First, the RowState will get set to Deleted.
Second,
| > any
| > | attempt to read a deleted row will throw an exception. This bothers me
| > | because it seems like calling Delete from for i= and from foreach
| > results
| > in
| > | different behavior - if it's just marking rows it doesn't seem like
| > calling
| > | Delete should have screwed up the enumerator
| > |
| > | i think i can hobble my code together but does anyone have any insight
| > as
| > to
| > | how this works and why?
| > |
| > | -baylor
| >
| >
|
|
 
Back
Top