9

If I have the following ASP.NET code (it's not complete - obviously there's a lot missing, but none of it matters):

    <asp:GridView>
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    My Label: <asp:Label />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    My Text Box: <asp:TextBox />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

And if I have something like this in the CodeBehind:

Private MyListOfObjects As List(Of MyObject)

...

Public Class MyObject

    Public MyTextBoxString as String
    Public MyLabelString as String

End Class

How can I bind the GridView so that one row is equivalent to one item in my MyListOfObjects list, and so that the data will populate and persist across page loads or postbacks? I've never done custom databinding like this before, so a full explanation would be very helpful. All the tutorials I've come across so far only talk about using GridViews directly with Database query results, and that's not what I need.

Thanks!

qJake
  • 16,821
  • 17
  • 83
  • 135
  • Check if this helps: http://weblogs.asp.net/gurusarkar/archive/2010/04/28/binding-list-of-custom-class-to-gridview-or-listview-control.aspx – gbs Apr 01 '11 at 21:19
  • see my response to your question. – JonH Apr 01 '11 at 23:03

3 Answers3

8

Just set the datasource of the gridview to your object.

MyGridView.DataSource = myList
MyGridView.DataBind()

Here's a very similiar post:

Binding a method that returns List<employee> to a gridview

Looks like you are using a list in vb.net. Remember lists can hold integers, strings, dates, objects (these include user defined types (your object)). So you can bind a gridview to a list object by setting the datasource property to your list.

In the above example, myList, might hold a ton of employee objects, etc. So assign it to the datasource and .DataBind() and voila a gridview with each row containing your object.

Community
  • 1
  • 1
JonH
  • 32,732
  • 12
  • 87
  • 145
  • Except I've predefined my columns beforehand, and my list doesn't match up to the columns, so I need to manually specify and map the rows to objects inside my list. – qJake Apr 01 '11 at 20:01
  • @SpikeX - You don't need to do anything, whatever columns you've `` from your grid view will be displayed. For instance, assume you have a customer object with 3 properties age, name, and birthdate. If you only want to show name, and age then in your .aspx markup have 2 templatefields that have name and age. My solution handles this for you. – JonH Apr 01 '11 at 23:03
  • Your answer was closest, but you didn't describe how to bind individual `TemplateFields` to the object's properties - you need to use `<%# Databinder.Eval(Container.DataSource, "MyPropertyName") %>` – qJake Apr 05 '11 at 19:46
  • [Darren Pope](http://stackoverflow.com/users/1243973/darren-pope) [comments](http://stackoverflow.com/suggested-edits/212641) "@SpikeX - If you are using the BoundField object, then you do not need to use the DataBinder.Eval. You need only set the DataField attribute to the corresponding Property. Plus, I think it's cleaner. One line of code rather than 5+ (if tabbed correctly)." – Rup Mar 02 '12 at 00:16
1

You can do something like

 My Label: <asp:Label id="myLabel" runat="server" Text='<%# Eval("MyTextBoxString") %>'  />

in the markup and similar stuff for your textbox.

GridView1.DataSource = MyListOfObjects
GridView1.DataBind()
Bala R
  • 107,317
  • 23
  • 199
  • 210
1

First remember any binding controls like GridView, DropdownList e.t.c bind to public properties, so first make your public members to public properties.

Then create objects of MyObject class and add them to your List<MyObject> object

Finally you can persist this list object by saving it in Session or ViewState to maintain it after postbacks.

I hope you can do it now!!! you can ask for more help if neccessary.

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38