DataSource

P

Peter Larsen [CPH]

Hi,

I use a List<object> as datasource to a DataGridView class.

The datasource class looks like this:
public class DataList : List<DataListItem>
{
...
}

Is it possible to "remove" or omit some entities from the list temporary ??

Lets say i have 100 items in the list and they are sorted after their
colors.
The user then choose to remove some of the colors from the list.
What i want to do, is to omit some of the items from the view (temporary
hiding) without removing them from the list. Is that posible ??

Thank you in advance.
BR
Peter
 
W

wdudek

Since you have a custom class, why not create a method that returns the
generic list of objects which can provide the list for the data bind. This
could then create a new copy of your source list filtered by whatever you
needed and return that to the calling method. Depending on the version of the
framework you are using you could do this through a foreacch loop, or much
cleaner using LINQ.
 
P

Peter Larsen [CPH]

Not a bad idea - it would work - easy and simple solution.
I'm using .Net 3,5 and LinQ in my code.

I still think there must be another way to do this - something that includes
an interface and override an enumeration method.
It must be using an enumerator when getting the data from the datasource. I
just don't know which.

/Peter
 
A

abilwd

I think you may consider storing the ¡°removed¡± status of each DataListItem
in the DataList class, and over-writing DataList¡¯s GetEnumerator function.
In the function, we go through each element of the list, check its
¡°removed¡± status, and ¡°yield¡± the element if its status is not
¡°removed¡±.
 
W

wdudek

The later reply sounds like it would provide a means to do this, my concern
however would be that you are then creating code that could be confusing to
later developers. When they databind to the list and don't get all of the
records they may not realize at first that there is some sort of filtering
going on. By explicitly calling a method whose name implies the results may
be filtered you do risk this issue.
 
P

Peter Larsen [CPH]

What i want is to allow the user to select a filter in a ContextMenu - its
not a permanent filter.
The default is that there is no filter applied.

I have found a class called "BindingSource" which contains a filter
propertry (haven't tried the filter property yet), but i think this is what
i need.
The class is placed in between the datasource and the viewer.

/Peter
 
W

wdudek

Yes I understand what you are trying to do, and using a class in between the
data source and the viewer sounds like a good solution. My point was that
embedding the filtering logic in the enumerator of a class could be confusing
to someone 5 years down the road who has to work on the same code and isn't
sure why they are not getting all of the results whent he enumerator is used.

Bill
 
P

Peter Larsen [CPH]

Hi Bill,

I agree with you on this, messing with the enumerator without some kind of a
warning, is a no-go.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top