Can a DataGrid use a collection?

G

Guest

Hello

Thanks for reviewing my question. I believe I read that a DataGrid can point to a collection but would like to know if you can point to an example. How does the DataGrid know what are the column headings? I created my own collection and would like a DataGrid on a Form to use it. For example

private class EventDetail

public EventDetails(string name) { this.sEventName = name;
public string EventName { get { return sEventName; }
private string sEventName
}

private class ScheduledEvents : IEnumerabl

public ScheduledEvents() { eventList.Add(new EventDetails("Event 1"));

public ArrayList eventList = new ArrayList()

public IEnumerator GetEnumerator() { return eventList.GetEnumerator();
}

DataGrid.DataSource = ScheduledEvents
DataGrid.DataMember = "EventName"

Is this possible

Many Thanks to the Expert
Pete
 
E

Erik Frey

Peter said:
private class ScheduledEvents : IEnumerable
{
public ScheduledEvents() { eventList.Add(new EventDetails("Event )); }

public ArrayList eventList = new ArrayList();

public IEnumerator GetEnumerator() { return entList.GetEnumerator(); }
};

DataGrid.DataSource = ScheduledEvents;
DataGrid.DataMember = "EventName";

It's certainly possible - and you're nearly there. Have your
ScheduledEvents class implement IList or IListSource. Or, just bind your
DataGrid to an ArrayList of EventDetails.

Either way works. For more info, read the DataGrid.DataSource property
documentation.

Erik
 
G

Guest

Erik,

Thanks for the reply. Since my ScheduledEvents is already derived from IEnumerable, how should I derive it from IList or IListSource - multi-inheritence? Also, did you mean ScheduleEvents.eventList because EventDetails does not have an array?

Thanks again
Peter
 
I

Inacap

Peter:

Yes, Datagrid can use a custom collection as a data source (Datasource
property), but your custom collection must implement certain interfaces. A
simple way to do this is inheriting from ReadOnlyCollectionBase class. This
class implemet ICollection and IEnumerable interfaces and provide its
subclasses with a protected ArrayList. (that is reusing framework classes).

I rename your collection with "suffix" Collection, so it's a framework
convenio.

//
private class EventDetailsCollection : ReadOnlyCollectionBase
{
//this class inherit a protected ArrayList field named InnerList
//and an overridable Count property

//Add Method
public void Add(EventDetails eventDetails){
InnerList.Add(eventDetails);
}

//an Indexer
public EventDetails this[int index]{
if(index>=0 && index<Count) return (EventDetails)InnerList[index];
return null;
}
};

LC



Peter said:
Hello,

Thanks for reviewing my question. I believe I read that a DataGrid can
point to a collection but would like to know if you can point to an example.
How does the DataGrid know what are the column headings? I created my own
collection and would like a DataGrid on a Form to use it. For example:
 
E

Erik Frey

Peter said:
Erik,

Thanks for the reply. Since my ScheduledEvents is already derived from
IEnumerable, how should I derive it from IList or IListSource -
multi-inheritence?

Peter,

IList inherits from ICollection and IEnumerable. This means that if your
class implements IList, it also inherantly implements IEnumerable. You get
it for free.
Also, did you mean ScheduleEvents.eventList because EventDetails does
not have an array?

Nope. I literally meant you can do away with a ScheduleEvents class
alltogether and do something like this.

ArrayList myArrayList = new ArrayList();
myArrayList.Add(new EventDetail("First Event"));
myArrayList.Add(new EventDetail("Second Event"));
myArrayList.Add(new EventDetail("Third Event"));
DataGrid.DataSource = myArrayList;

Or, you can follow Inacap's very good advice below and create a collection
that derives from either CollectionBase or ReadOnlyCollectionBase - this way
you can ensure strong typing. Using an ArrayList is kind of a "sloppy" way
of doing it.

Erik
 
D

Daniel Grossztein

Hi, I try to put a arraylist into an arraylist to make a hierarchical
datagrid.
It's works fine but when I press the (+) expand icon, i see a link to a
related table instead the related items of the collection.
How can I change this behavior?

Thanks

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

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