1. Original Entry + Comments2. Write a Comment3. Preview Comment
New comments for this entry are disabled.

February 03, 2006  |  GridView and dynamic data sources  |  18160 hit(s)

Now and again you want to bind the GridView control not to a data source control or dataset, but just to some data you happen to have hanging around. A typical example, for example, might be that you extract or generate the data at run time -- i.e., it's not in a data store -- and then want to display it.

As the docs say, you can bind data control to anything that implements IEnumerable. All well and good, but it isn't always obvious how to bind to some arbitrary but IEnumerably-implementing data structure.

The question came up at work the other day, and the awesome Polita (who in addition to being the author of the GridView control is also very generous with her expertise) clarified this for some of us. So here's what I learned.

One possibility is that you are working with objects of some type, and you have a collection of them that you want to display in the GridView control. Let's say you have Customer objects. You put a bunch of them into an ArrayList (or, as noted, anything else that can enumerate). To bind the GridView to this array, you set its DataSource property to the array and, of course, call DataBind. To display the data, you can create BoundField objects (in code or declaratively), and you set their DataField property to the name of the property that you want to display. Here's what the GridView markup might look like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<asp:BoundField HeaderText="ID" DataField="CustId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="City" DataField="City" />
And here's the code (slightly verbose, sorry) for a simple Customer type and for creating several of them, adding them to an ArrayList, and then binding the GridView to the array:
   Class Customer
Dim _custid As Integer
Dim _name As String
Dim _city As String
Property CustId() As Integer
Return _custid
End Get
Set(ByVal value As Integer)
_custid = value
End Set
End Property
Property Name() As String
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Property City() As String
Return _city
End Get
Set(ByVal value As String)
_city = value
End Set
End Property

Sub New(ByVal custid, ByVal name, ByVal city)
_custid = custid
_name = name
_city = city
End Sub
End Class

Sub Page_Load()
Dim cust1 As New Customer(1, "AAA Repair", "Eugene")
Dim cust2 As New Customer(2, "Bee's Knees", "Woucester")
Dim cust3 As New Customer(3, "Chop-Chop Restaurant", "Seattle")

Dim custArray As New ArrayList()
GridView1.DataSource = custArray
End Sub
Sometimes your life is even simpler -- you have an array with simple values in it, and you want to display those. It's easy enough to bind the grid to the array. But how do you bind a BoundField to the contents of the array element? Turns out that if you set the DataField property of a BoundField to !, it tells the field (column) to get the ToString() version of whatever's in the array. (I'm pretty sure this is undocumented, though perhaps not unknown.)

So here's some GridView markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<asp:BoundField HeaderText="Array Field" DataField="!" />
And here's some code to create an array and bind the grid to it:
Dim a As New ArrayList()
GridView1.DataSource = a
So there's a lesson in simple binding to dynamic data. (Or is it binding to simple dynamic data?)

Andy   05 Jun 06 - 5:01 AM

Thanks Mike. I've looking about, trying to field what to put in a DataField attribute when an Array List is bound to a GridView. An exclamation mark worked a treat.

Yax   07 Sep 06 - 6:34 AM

Thanks Mike!

I will love you forever. This was precise and short and - even better - IT WORKS!

igor   30 Dec 06 - 11:48 AM

It works.

moredotnet   20 Mar 07 - 10:38 PM

Really very precise, accurate & efficient.

B.   12 Apr 07 - 7:23 PM

Now if it could only sort and page....

Crazee   02 May 07 - 6:35 AM

How can i bind a HyperLink column if the DataSource in an Array ?
BoundField works, But i need a HyperLinkField

Wei Chieh   26 Jul 07 - 2:15 AM

Just wanted to thank you for this - it really helped me out! I hope you don't mind, I've linked to this post from my own blog, because I had a hell of a time looking for something just like this and would like to help more people find you! Again, thank you!

Ryan   28 Sep 07 - 9:41 AM

Question. This solution worked for when I wanted to Bind my ArrayList to a Gridview using an ObjectDataSource. How do I write a Delete Method for this same DataSource when I can't use ! as the field name.

J   30 Oct 07 - 12:37 PM

this will take care of sorting and paging

saurav   29 Mar 08 - 4:47 AM

thats ok but can u please tell me how to do the same if i have several tables that i need to bind to the gridview at runtime based on some click event??

sonix   14 Jul 08 - 2:36 PM

Thank you for this hint with the !
I miss this since ever!