Populate a UserControl Gridview with a List of Objects

后端 未结 4 522
傲寒
傲寒 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:58

    I don't really understand the sentence, "I want to use this to fill a UserControl of a Gridview."? However, if the question you are asking is how to bind a GridView to a list of your Reasons objects then this should work:

    .aspx Page

    Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {
        List reasonsList = new List()
        {
            new Reasons() { Code = "Code 1", Text = "Text 1" },
            new Reasons() { Code = "Code 2", Text = "Text 2" },
            new Reasons() { Code = "Code 3", Text = "Text 3" },
        };
    
        GridView1.DataSource = reasonsList;
        GridView1.DataBind();
    }
    
    public class Reasons
    {
        public string Text { get; set; }
        public string Code { get; set; }
    }
    

提交回复
热议问题