Databinding to Custom Objects
page 2 of 4
by J. Ambrose Little
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25709/ 63

My Collection Object
 
1:
/// <summary>
2:
/// The knights of the round table.
3:
/// </summary>
4: public class KnightsOfTheRoundTable : System.Collections.IEnumerable
5: {
6:
/// <summary>
7:
/// ArrayList -- handles all the details for the collection.
8:
/// </summary>
9: private System.Collections.ArrayList knights =
new System.Collections.ArrayList();
10:
11: #region IEnumerable Members
12:
/// <summary>
13:
/// Implementation of IEnumerable: Gets an Enumerator.
14:
/// </summary>
15:
/// <returns>An Enumerator of the collection.</returns>
16: public IEnumerator GetEnumerator()
17: {
18: return this.knights.GetEnumerator();
19: }
20: #endregion
21:
22:
/// <summary>
23:
/// Gets or sets a knight by his position at the table.
24:
/// </summary>
25: public KnightOfRoundTable this[int index]
26: {
27: get
28: {
29: if (this.knights.Count > index)
// that position exists
30: return (KnightOfRoundTable)this.knights[index];
31: else
32: throw new ArgumentException(
"That position at the table has not been filled yet!");
33: }
34: set
35: {
36: if (this.knights.Count > index)
// that position exists
37: this.knights[index] = value;
38: else
// it doesn't exist
39: throw new ArgumentException(
"That position at the table has not been filled yet!");
40: }
41: }
42:
43:
/// <summary>
44:
/// Gets or sets a knight by his name.
45:
/// </summary>
46: public KnightOfRoundTable this[string name]
47: {
48: get
49: {
50:
// try to find knight with given name
51: for (int i = 0; i < this.knights.Count; i++)
52: {
53:
// return knight if found
54: if (((KnightOfRoundTable)this.knights[i]).Name.ToUpper()
== name.ToUpper())
55: return (KnightOfRoundTable)this.knights[i];
56: }
57:
// if not found, let them know about it
58: throw new ArgumentException(
"No such knight is honored at our table!");
59: }
60: set
61: {
62:
// try to find knight with given name
63: for (int i = 0; i < this.knights.Count; i++)
64: {
65: if (((KnightOfRoundTable)this.knights[i]).Name.ToUpper()
== name.ToUpper())
66: {
67:
// set found knight to be new knight
68: this.knights[i] = value;
69: return;
70: }
71: }
72:
// if not found, let them know about it
73: throw new ArgumentException(
"No such knight is honored at our table!");
74: }
75: }
76:
77:
/// <summary>
78:
/// Add a knight swearing fealty to Arthur.
79:
/// </summary>
80:
/// <param name="knight">Brave cavalier joining
forces with Arthur.</param>

81: public void Add(KnightOfRoundTable knight)
82: {
83: this.knights.Add(knight);
84: }
85:
86:
/// <summary>
87:
/// Remove a knight who has fallen from grace.
88:
/// </summary>
89:
/// <param name="knight">Knight in disgrace.</param>
90: public void Remove(KnightOfRoundTable knight)
91: {
92: this.knights.Remove(knight);
93: }
94: }
Notice on Line 4 that I'm specifying this class implements the IEnumerable interface--this is key for data binding. You can see on Line 9 that I declare and initialize my private ArrayList. On Lines 16-19, I implement the IEnumerable interface by simply returning the enumerator from my ArrayList. As mentioned, this is the critical part for data binding; the rest of the code is more or less arbitrary, although you will want to at least provide a way to add new objects to the collection as I have in Lines 81-84.

Everything else is pretty much up to you. I chose to add two indexers, one by index (Lines 25-41) and one by name (Lines 46-75). I also provided a Remove method on Lines 90-93. You may notice that in the Add/Remove methods, I'm simply calling the corresponding methods on the ArrayList. In the integer indexer, I guard against an ugly out of bounds exception and instead provide a more thematic exception if the position is not available. In the name indexer, I cycle through the current knights and again throw a thematic exception if the desired object is not found.

 


View Entire Article

User Comments

Title: help   
Name: Francisco Cruz
Date: 2009-12-17 3:11:43 PM
Comment:
Object Persistance between postbacks
Title: Very helpful   
Name: Ben
Date: 2008-09-15 2:22:35 PM
Comment:
just what I needed thanks
Title: Mr   
Name: Phillip Knezevich
Date: 2007-02-06 3:45:56 PM
Comment:
It's a geeky example, but I found it useful.
Title: Serialization   
Name: Ambrose
Date: 2006-09-21 7:49:08 PM
Comment:
Russ B,

I guess you're trying to use XML serialization like in a Web Service or directly? If so, yeah, it needs an Add method for when it deserializes so that it can add the deserialized instances to the new collection. You just need to implement an Add method, as far as I recall.
Title: Serialization   
Name: Russ B
Date: 2006-09-20 4:25:10 PM
Comment:
This is exactly what I've been looking for too.

I tried to serialize your KnightsOfTheRoundTable class and got this error: "System.InvalidOperationException: You must implement the Add(System.Object) method on BizDataBinding.KnightsOfTheRoundTable because it inherits from IEnumerable."

But you did implement Add. Any idea what else needs to be implemented?

...I'm still in 2003
Title: Binding to Complex Properties   
Name: J. Ambrose Little
Date: 2005-03-08 9:36:56 AM
Comment:
Hi Mark,

What I was saying is that if you just bind to the Color property, it will print out something like "Color [Black]" instead of the color name, which is really what we're after in this example. So in order to print out the value you want, just access the property of the property that you want to display, e.g., Color.Name. I hope this helps.
Title: Thanks!   
Name: Mark Miller
Date: 2005-03-08 12:27:37 AM
Comment:
I was looking around the Internet for an article like this. I'm a bit surprised it was not easier to find.

After finishing my first big web project, I wish I had used a technique like this. Unfortunately too many databinding examples online and in the MSDN docs just show you how to bind to a dataset.

I am a bit confused about the last part, where you say, "So if you do not have control over the ToString method on your class members' types, you may need to use this syntax to specify how you want the bound item to render that member." It seems you're referring to the string "Color [ColorName]", "ColorName" being the name of the color, but I don't know what this syntax would mean in relation to databinding. Aren't you trying to extract the name of the color so that it can display in whatever you are databinding to? What does the "Color [ColorName]" syntax accomplish?
Title: Using CollectionBase   
Name: Scott M
Date: 2004-12-12 4:19:12 PM
Comment:
Hey J,

Thanks for the article. I found it really useful. I read (in hardcopy) and article about inheriting from CollectionBase the other day and thought I'd let you know, turns out by your comment that you found it too!

So anyway, cheers and roll on generics,
Scott.
Title: Using CollectionBase   
Name: J. Ambrose Little
Date: 2004-08-31 12:53:30 PM
Comment:
Glad the article helped. I've actually found (since this article was published) that inheriting from CollectionBase is even easier than just implementing IEnumerable for my strongly-typed collections. Now I can't hardly wait for 2.0 where we won't even need to bother declaring our own collections and can use the generic ones instead!
Title: Thanks   
Name: Scott
Date: 2004-08-31 1:37:07 AM
Comment:
Hey Mr Little, great article. I was a little confused about implementing IEnumerable but once I dissected your example I managed to get my productset (using a dataset stored internally) going to bind it to a asp:repeater... I implemented ienumerator too but your article got me started, so cheers for putting it online.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-25 10:26:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search