About

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

Read more ...

Blog Search


(Supports AND)

Google Ads

Feed

Subscribe to the RSS feed for this blog.

See this post for info on full versus truncated feeds.

Quote

Paper's Law: It's not that simple.

Herb Paper, via John Lawler



Navigation





<July 2014>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

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  

Contact

Email me

Blog Statistics

Dates
First entry - 6/27/2003
Most recent entry - 7/23/2014

Totals
Posts - 2304
Comments - 2488
Hits - 1,647,495

Averages
Entries/day - 0.57
Comments/entry - 1.08
Hits/day - 407

Updated every 30 minutes. Last: 8:34 PM 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] |