Link to home
Start Free TrialLog in
Avatar of Chris Dent
Chris DentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB .NET: Binding custom IList to GridView

When I started playing with this earlier today it looked like such a simple thing. Except I still can't get it to work. There's a lot of information around about doing this, I seem to find the bits which are either in C# or I just hear a whooshing sound as things go over my head (the latter being preceded by a glazed expression).

Ah well... hopefully I'm just missing something or my syntax is wrong.

I have an IList defined like this:

Public ListOfStuff As New List(Of BitsOfStuff)

BitsOfStuff is a Structure defined like this:

Public Structure BitsOfStuff
        Dim GreenStuff As String
        Dim BlueStuff As String
End Structure

In my sample code there are two members in BitsOfStuff. I can evaluate and display them if I use traditional looping type statements.

I was vaguely (very vaguely) under the impression I could bind that IList to a GridView control with:

SomeGridView.DataSource = ListOfStuff
SomeGridView.DataBind()

It doesn't complain at me, but I can't get it to display anything.

So far I've tried:

<asp:BoundField DataField="GreenStuff" HeaderText="Green Stuff" />

But it tells me GreenStuff doesn't exist.

I've also tried (and completely failed) to use a number of different statements within a TemplateField.

<asp:TemplateField HeaderText="Blue Stuff">
    <ItemTemplate>
        <%# DataBinder.Eval(BitsOfStuff, "DataItem.BlueStuff") %>
    </ItemTemplate>
</asp:TemplateField>

Is it something really simple? Or am I just doing it completely wrong?

Thank you in advance.

Chris
ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Whoops, my apologies, ignore all that, lists do implement IEnumerable. I think the problem is that the values must be implemented as properties rather than just public variables.

I created a list of the structure in the code snippet above and assigned it as the gridview's datasource and it worked fine. When I changed the properties back to be public variables it didn't work.
Avatar of Chris Dent

ASKER


Ahh that's a good start to the day. It works perfectly now, thank you very much :-D

Chris
Exactly what I needed :) Thank you.

Chris