There is just one thing that must be considered for the data binding. That is to extract the data from the objects that you may receive from the collection. This can be simply done by falling back onto the famous DataBinder class:
Depending on the type of object (“obj” in the above code example), it will determine the amount of reflection that will occur to get the value. If it is a DataRow class or DataRowView class (from the DataTable and DataView, respectively, the less reflection that will occur; however, for any other object, more reflection will be involved to get the value from the property (the name of the property is determined by the “ColumnName” parameter).
With all that said, you should deal with the data that will be used. For example, your control only needs to deal with two columns and a data reader is issued to us with ten columns, then we only need to save the two columns of every record to the internal data source. That will reduce the view state size greatly.
You might have a server control like the DataGrid control (or other template based server control). That will pose an additional obstacle that you may run into is that you want to save all the information that is presented in an object. This problem can be easily overcome by using Reflection. I will leave it at that point because it goes way out of scope of this article… in fact, you may have a look at the following articles:
Understanding Reflection – Part One
Understanding Reflection – Part Two
The last article is not needed for data binding to an entire collection; however, you will get a deeper understanding of .NET from this article. Just a few tips though:
- Look at listing properties in that object that you can read.
- Do not consider looking at objects and properties that their access level is not “public”. Your control will run super slow if you do so.