Since the introduction of Generics, the .NET framework has seen many different
collections of strongly typed objects. The process of binding those collections
to a GridView is as simple as setting the source to the collection and calling
the bind function. Recently I found myself adding custom code to convert the
collections to DataViews so that I could assign my filters and let the View take
care of the rest. In this article I will try to cover the problem, design, and
solution implemented.
Problem
The problem was briefly stated in the introduction. Basically everytime a
collection needed a view I found myself writing code like the one below.
|
DataTable
tbCounties = new System.Data.DataTable();
tbCounties.Columns.Add("CountyID",
typeof(int));
foreach
(County county
in counties)
view =
new DataView(tbCounties); |
In a small collection like the example above,
coding it everytime wouldn't take long. The problem would be when a collection
contained ten or more properties or worse yet, that the code would not be
reusable for other collections without some work.
Design
The design needed to resolve my problem needed to be simple and extensible. The
first thing that came to mind was how to leverage Reflection to do some object
exploration and come up with a DataView. Once I had a good idea of what I could
retrieve with Reflection and how to use the functionality, the last step was
exposing it for all my collections to leverage. The final design decision
indicated the delivery of the functinality through and extension method called
ToView.
Solution and Implementation
With a clear idea of what the end goal was. The remaining task involved figuring
out what type to extend so that all the collections were extended without the
method needing to know about any particular type. A common interface found
implemented by many of the Generic templates like List<T> is the IList
interface.
|
///
<summary>|
// Initialize DataTable and get
all properties from the first Item in the List.
// Add all rows
for (int
i = 0; i < row.Length; i++)
table.Rows.Add(row); |
The listing above contains the entire listing
for the extension. Attention was kept to the limitations of a DataTable in
regards to Null able types. In the case of a NotSupportedException, it was
assumed that we needed the underlying type of a Nullable object instead of
casting it to an object.
Closing
The .NET framework cotains many features and functinality that we could combine
as developers to solve everyday common problems. Extending a collection to
improve functionality may not always work the first time, but braking up the
task into different modules will make the task less daunting.

Mahesh ChandPosted Feb 24, 2011, 9:18 AM
Good!