Remove Row from a DataView

  • Thread starter Thread starter MarkusJNZ
  • Start date Start date
M

MarkusJNZ

Hi, I have a dataview which I am using to bind to a gridview.

In the page load event I am checking for a condition in each row of
the dataview and if it exists I want to remove the row from the
dataview before I bind it to the gridview;

Pseudo code below
================

DataView people = ExecuteDataSet.Tables[0].DefaultView;

for(int nloop = 0; nloop<people.Count; nloop++)
{

if(people[nloop]["Age"] < 18)
{
// Remove the currrent row from the DataView
}

}
================

I have tried

people.Table.Rows.RemoveAt(nloop);
people.AcceptChanges();

but this does not seem to work

Any help appreciated!
Thanks
Markus
 
but this does not seem to work

So what *does* happen? Does the count change?

First - you need to be very careful removing from a loop you are
iterating - the code you have is susceptible to cumulative "off by
one" errors every time it deletes - so with 2 adjacent youngsters only
the 1st would disappear. Perhaps iterate backwards instead? (or see
below).

There is no guarantee that the order is the same in the view and the
table (if a sort is set on the view), so if the wrong rows are
disappearing perhaps you should be removing from the view (which will
talk to the table).

Perhaps an easier option is to set a RowFilter against the view? that
way they simply don't appear, as opposed to removing them from the
system just because they can't buy beer (or whatever).

Marc
 
So what *does* happen? Does the count change?

First - you need to be very careful removing from a loop you are
iterating - the code you have is susceptible to cumulative "off by
one" errors every time it deletes - so with 2 adjacent youngsters only
the 1st would disappear. Perhaps iterate backwards instead? (or see
below).

There is no guarantee that the order is the same in the view and the
table (if a sort is set on the view), so if the wrong rows are
disappearing perhaps you should be removing from the view (which will
talk to the table).

Perhaps an easier option is to set a RowFilter against the view? that
way they simply don't appear, as opposed to removing them from the
system just because they can't buy beer (or whatever).

Marc

Hi Marc, thanks. I did think that I was getting off by one errors;

I will just use rowfilter

Thanks for your help
Markus
 
Why not just use the rowfilter on the dataview?



Posted as a reply to:

Remove Row from a DataView

Hi, I have a dataview which I am using to bind to a gridview

In the page load event I am checking for a condition in each row o
the dataview and if it exists I want to remove the row from th
dataview before I bind it to the gridview

Pseudo code belo
===============

DataView people = ExecuteDataSet.Tables[0].DefaultView

for(int nloop = 0; nloop<people.Count; nloop++


if(people[nloop]["Age"] < 18

// Remove the currrent row from the DataVie



===============

I have trie

people.Table.Rows.RemoveAt(nloop)
people.AcceptChanges()

but this does not seem to wor

Any help appreciated
Thank
Markus

EggHeadCafe - Software Developer Portal of Choice
WCF Workflow Services Using External Data Exchange
http://www.eggheadcafe.com/tutorial...a-6dafb17b6d74/wcf-workflow-services-usi.aspx
 
Try

DataView dv = new DataView(dt);
List<DataRow> rowsToRemove = new List<DataRow>();
for (int i = 0; i < dv.Count; i++)
{
rowsToRemove.Add(dv.Table.Rows);
}
foreach (DataRow dr in rowsToRemove)
{
dv.Table.Rows.Remove(dr);
}
dataGridView1.DataSource = dv;

When you delete a by removeat other rows indexes changes. So it's better
deleting rows by datarow reference.

in message news:[email protected]...
 
Jim said:
Why not just use the rowfilter on the dataview?



Posted as a reply to:

Remove Row from a DataView

....


EggHeadCafe - Software Developer Portal of Choice
WCF Workflow Services Using External Data Exchange
http://www.eggheadcafe.com/tutorial...a-6dafb17b6d74/wcf-workflow-services-usi.aspx

Is your post coming from egghead cafe, or did the original post? If
your post came from there, there is some bug in their system of late
where the posts do not attach to the original thread. This means if
there were other posts, we cannot see them (well, at least without
resorting to investigative techniques). This has happened to several
messages over the past week or so.
 
Back
Top