469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

How to delete rows?

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
Jul 22 '05 #1
4 10185
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
"baylor" <ba****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
|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
Jul 22 '05 #2

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[i].delete();
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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
"baylor" <ba****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
|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

Jul 22 '05 #3
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
Jul 22 '05 #4
Richard,
Why? The sample I gave works for DataTables! As does the more general
approach described by Cor.

Hope this helps
Jay
"Richard P" <or**@community.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
|
| 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[i].delete();
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:%2******************@tk2msftngp13.phx.gbl...
| > 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
| >
| >
| > "baylor" <ba****@discussions.microsoft.com> wrote in message
| > news:F7**********************************@microsof t.com...
| > |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
| >
| >
|
|
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gordon | last post: by
16 posts views Thread by robert | last post: by
2 posts views Thread by NoSpam | last post: by
9 posts views Thread by Dejan | last post: by
8 posts views Thread by Michel Esber | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.