winForms + DataGridView binding to a List

后端 未结 4 870
夕颜
夕颜 2020-12-29 20:15

I\'m trying to bind a List to a DataGridView control, and I\'m not having any luck creating custom bindings.

I have tried:

gvPr         


        
4条回答
  •  失恋的感觉
    2020-12-29 20:27

    Is the property on the grid you are binding to Opcode as well?.. if you want to bind directly to List you would just DataSource = list. The databindings allows custom binding. are you trying to do something other than the datasource?

    You are getting a bunch of empty rows? do the auto generated columns have names? Have you verified data is in the object (not just string.empty) ?

        class MyObject
        {
            public string Something { get; set; }
            public string Text { get; set; }
            public string Other { get; set; }
        }
    
        public Form1()
        {
            InitializeComponent();
    
            List myList = new List();
    
            for (int i = 0; i < 200; i++)
            {
                string num = i.ToString();
                myList.Add(new MyObject { Something = "Something " + num , Text = "Some Row " + num , Other = "Other " + num  });
            }
    
            dataGridView1.DataSource = myList;
        }
    

    this should work fine...

提交回复
热议问题