Populate a UserControl Gridview with a List of Objects

后端 未结 4 498
傲寒
傲寒 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 11:02

    I am assuming you're doing this in winform C#. It should be fairly similar in C# codebehind for asp.net. Here's some sample code you can easily customize to your obj type:

    /// 
    /// The test class for our example.
    /// 
    class TestObject
    {
        public string Code { get; set; }
        public string Text { get; set; }
    }
    
    void PopulateGrid()
    {
        TestObject test1 = new TestObject()
        {
        Code = "code 1",
        Text = "text 1"
        };
        TestObject test2 = new TestObject()
        {
        Code = "code 2",
        Text = "text 2"
        };
        List list = new List();
        list.Add(test1);
        list.Add(test2);
    
        dataGridView1.DataSource = list;
    }
    

提交回复
热议问题