Populate a UserControl Gridview with a List of Objects

后端 未结 4 524
傲寒
傲寒 2021-01-03 10:05

I have a List of an object called \"Reasons\" that contains two properties \"Code\" & \"Text\". I want to use this to fill a UserControl of a Gridview. However, I don\

4条回答
  •  暖寄归人
    2021-01-03 10:59

    You're correct in your assumption that you set the DataSource to the List. You also need to remember to call the GridView.DataBind() command once you've set the DataSource to your list.

    i.e.:

    List lReasons = Assign List Here....
    
    gvReasons.DataSource = lReasons;
    gvReasons.DataBind();
    

    Also, If you want to setup your two properties as the columns in your GridView, assign them like so (assuming you're working with WebForms here and not WinForms):

    
    
        
        
    
    
    

    Or you can specify AutoGenerateColumns="true" and let the framework generate the columns itself.

提交回复
热议问题