Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB .NET: Binding custom IList to GridView

Posted on 2008-06-25
4
Medium Priority
?
2,893 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:Chris Dent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
NazoUK earned 2000 total points
ID: 21869643
In order to be bindable to something like a gridview, an object must implement the Ienumerable interface.

Probably the easiest way to do this would be by using an objectdatasource control which will automatically wrap your object as an Ienumerable if it doesn't natively support it. I had a play about and got a gridview to display your structure correctly. See code snippet for what I did.
BitsOfStuff.vb:
 
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
 
Public Structure BitsOfStuff
    Private _GreenStuff As String
    Private _BlueStuff As String
 
    Public Property GreenStuff()
        Get
            Return _GreenStuff
        End Get
        Set(ByVal value)
            _GreenStuff = value
        End Set
    End Property
 
    Public Property BlueStuff()
        Get
            Return _GreenStuff
        End Get
        Set(ByVal value)
            _GreenStuff = value
        End Set
    End Property
 
    Public Function getStuff() As List(Of BitsOfStuff)
        Dim ListOfStuff As New List(Of BitsOfStuff)
        For i As Integer = 1 To 10
            Dim Bit As New BitsOfStuff
            Bit.BlueStuff = "Blue" & i
            Bit.GreenStuff = "Green" & i
            ListOfStuff.Add(Bit)
        Next
        Return ListOfStuff
    End Function
End Structure
 
default.aspx:
 
<asp:GridView runat="server" ID="SomeGridView" DataSourceID="ods" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="GreenStuff" />
        </Columns>
        </asp:GridView>
        <asp:ObjectDataSource runat="server" ID="ods" TypeName="BitsOfStuff" SelectMethod="getStuff" />
       

Open in new window

0
 
LVL 15

Expert Comment

by:NazoUK
ID: 21869735
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.
0
 
LVL 71

Author Comment

by:Chris Dent
ID: 21873012

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

Chris
0
 
LVL 71

Author Closing Comment

by:Chris Dent
ID: 31470626
Exactly what I needed :) Thank you.

Chris
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question