Populating a programatically declared datagridview using datasource and entity framework

断了今生、忘了曾经 提交于 2019-12-06 07:01:23

Your code should be work. below is sample code to bind List<T> to DataGridView , but you need to add the DataGridView to the form or some other panel ( or container)

    public Form1()
    {
        InitializeComponent();
        DataGridView gv = new DataGridView();
        gv.DataSource = new List<string>() { "sss", "aaa" }.Select(x => new { Name = x }).ToList();
        this.Controls.Add(gv); // add gridview to current form or panel ( or container), then only it will display 
    }

At first there is no grid in my design. Iam adding grid also in dynamically

        AmolEntities db=new AmolEntities();

        DataGrid dataGridView1 = new DataGrid();
        this.Controls.Add(dataGridView1);  

        var v= from n in db.oe_subjects select n;
        dataGridView1.DataSource = v.ToList();

When creating object of datagridview you need to set

dataGridView1.AutoGenerateColumns = true

Please make sure you have done this before assigning datasource to it.

Winforms datagridview only generates columns if its actually part of the GUI. By sticking all of my datagridviews on the form in the form designer, the datagridviews generated the columns as I needed. I didn't actually need the form so I just didn't call the show() method. Seems like a bit of a hackish way of dealing with the issue but that's how I fixed this

I have done easily First declared a new variable of the same DataTable and then I put tableAdapter.Fill(dt); and then dbGrid.DataSource = dt;

And it worked fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!