问题
just having some small issues with c# winforms datagridview. Here's my scenario
I am using entity framework and am trying to bind certain entity models to my datagridview datasource.
var query = from q in context.foo
select q;
dgv_Disp.DataSource = query.ToList();
When I ran this piece of code above on a form class which had a datagridview in the GUI, it all worked fine. The datagridview will automatically generate columns and the number of rows.
But when I run this exact same code with the exception that I don't have a datagridview in the GUI, I just declare it programmatically and then set the datasource like the above code. And when I do it this way, no rows or columns are generated.
What is the difference between these two different datagridviews? I know that there are properties set in the designer.cs file of the form class. But I tried copying these settings and it still won't populate.
I know it's probably something simple but I just can't figure this out at all. If someone could show me what im doing wrong, that'd be great!
Edit
I have used AutoGenerateColumns = true but it didn't make any difference. Also I'm not actually trying to display this datagridview, I was just binding it to entity objects that way I could access its members using a string index. But I dont want to query the database just to get my info in a datagridview specific format because in my actual scenario, I already have my entity data from a previous query. I was just using the above code as an example.
回答1:
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
}
回答2:
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();
回答3:
When creating object of datagridview you need to set
dataGridView1.AutoGenerateColumns = true
Please make sure you have done this before assigning datasource to it.
回答4:
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
回答5:
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
来源:https://stackoverflow.com/questions/18503956/populating-a-programatically-declared-datagridview-using-datasource-and-entity-f