Published: 30 May 2003
This has got to be one of the coolest things I've done with ASP.NET in quite some time. I'm binding a collection of my own custom business objects to a DataGrid. No data views, no data readers, only the objects that I made, the way I like them. Even Burger King can't beat this!
What's funny is that it's really quite simple, and had I taken the time to figure it out sooner, I could have been eating Whoppers for a long time now. All you need to do is create a collection class that implements IEnumerable, create a private field as an ArrayList, provide accessors to at least add your custom object to the collection, and of course implement the IEnumerable interface by returning the enumerator from your ArrayList's GetEnumerator method. Then you can use the new collection class as the DataSource for your data list controls.
My Custom Object
1:
2:
3:
4: public class KnightOfRoundTable
5: {
6: private string name = string.Empty;
7: private System.Drawing.Color color = System.Drawing.Color.Black;
8: private string quest = string.Empty;
9:
10:
11:
12:
13: public string Name
14: {
15: get
16: {
17: return this.name;
18: }
19: set
20: {
21: this.name = value;
22: }
23: }
24:
25:
26:
27:
28: public System.Drawing.Color Color
29: {
30: get
31: {
32: return this.color;
33: }
34: set
35: {
36: this.color = value;
37: }
38: }
39:
40:
41:
42:
43: public string Quest
44: {
45: get
46: {
47: return this.quest;
48: }
49: set
50: {
51: this.quest = value;
52: }
53: }
54: }
55:
As you can see, it's quite a simple object. Just three members, two string and one Color.