How do I bind a GridView to a custom object?

后端 未结 3 2075
耶瑟儿~
耶瑟儿~ 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 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.

提交回复
热议问题