About

I'm Mike Pope. I live in the Seattle area. I've been a technical writer and editor for over 35 years. I'm interested in software, language, music, movies, books, motorcycles, travel, and ... well, lots of stuff.

Read more ...

Blog Search


(Supports AND)

Feed

Subscribe to the RSS feed for this blog.

See this post for info on full versus truncated feeds.

Quote

A person usually has two reasons for doing something: a good reason and the real reason.

— Thomas Carlyle



Navigation





<April 2025>
SMTWTFS
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Categories

  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  

Contact Me

Email me

Blog Statistics

Dates
First entry - 6/27/2003
Most recent entry - 4/4/2025

Totals
Posts - 2656
Comments - 2678
Hits - 2,734,977

Averages
Entries/day - 0.33
Comments/entry - 1.01
Hits/day - 344

Updated every 30 minutes. Last: 2:26 AM Pacific


  01:03 AM

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">
<Columns>
<asp:BoundField HeaderText="ID" DataField="CustId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="City" DataField="City" />
</Columns>
</asp:GridView>
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
Get
Return _custid
End Get
Set(ByVal value As Integer)
_custid = value
End Set
End Property
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Property City() As String
Get
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()
custArray.Add(cust1)
custArray.Add(cust2)
custArray.Add(cust3)
GridView1.DataSource = custArray
GridView1.DataBind()
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">
<Columns>
<asp:BoundField HeaderText="Array Field" DataField="!" />
</Columns>
</asp:GridView>
And here's some code to create an array and bind the grid to it:
Dim a As New ArrayList()
a.Add("Adam")
a.Add("Barney")
a.Add("Charlie")
a.Add("David")
GridView1.DataSource = a
GridView1.DataBind()
So there's a lesson in simple binding to dynamic data. (Or is it binding to simple dynamic data?)

[categories]   ,

[11] |