How to add data to DataGridView

后端 未结 5 1790
野的像风
野的像风 2021-01-02 10:09

I\'m having a Structure like

 X={ID=\"1\", Name=\"XX\",
    ID=\"2\", Name=\"YY\" };

How to dump this data to a DataGridView o

5条回答
  •  醉话见心
    2021-01-02 10:15

    Let's assume you have a class like this:

    public class Staff
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    

    And assume you have dragged and dropped a DataGridView to your form, and name it dataGridView1.

    You need a BindingSource to hold your data to bind your DataGridView. This is how you can do it:

    private void frmDGV_Load(object sender, EventArgs e)
    {
        //dummy data
        List lstStaff = new List();
        lstStaff.Add(new Staff()
        {
            ID = 1,
            Name = "XX"
        });
        lstStaff.Add(new Staff()
        {
            ID = 2,
            Name = "YY"
        });
    
        //use binding source to hold dummy data
        BindingSource binding = new BindingSource();
        binding.DataSource = lstStaff;
    
        //bind datagridview to binding source
        dataGridView1.DataSource = binding;
    }
    

提交回复
热议问题