How do I bind a GridView to a custom object?

后端 未结 3 2055
耶瑟儿~
耶瑟儿~ 2020-12-10 15:50

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

    
        

        
相关标签:
3条回答
  • 2020-12-10 15:51

    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.

    0 讨论(0)
  • 2020-12-10 15:53

    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.

    0 讨论(0)
  • 2020-12-10 16:03

    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()
    
    0 讨论(0)
提交回复
热议问题