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
-----------------------------------------------Reply-----------------------------------------------
On Jun 7, 2:26 pm, Marc Gravell <marc.grav
@gmail.com> wrote:
> > 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
Hi Marc, thanks. I did think that I was getting off by one errors;
I will just use rowfilter
Thanks for your help
Markus