2

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.

Vance Palacio
  • 1,280
  • 12
  • 17

5 Answers5

2

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 
    }
Damith
  • 62,401
  • 13
  • 102
  • 153
  • I dont actually want to display the data, is there some sort of container i can just declare and add it to that? – Vance Palacio Aug 30 '13 at 03:27
  • it is unclear why you need DataGridView if you don't need to display data, what exactly you want to do? – Damith Aug 30 '13 at 03:31
  • I have a list of strings in one table that refer to columns in another table. I wanted to bind the 2nd table to a datagridview that way I could access the values doing dgv.rows[0].Cells[index]. If the data remains in a straight entity, I can't access the data using a string. I know it's not really standard usage for a datagridview but it makes things much easier for me lol – Vance Palacio Aug 30 '13 at 05:28
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();
Boss
  • 445
  • 2
  • 8
  • 24
1

When creating object of datagridview you need to set

dataGridView1.AutoGenerateColumns = true

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

Ronak Patel
  • 2,570
  • 17
  • 20
0

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

Vance Palacio
  • 1,280
  • 12
  • 17
0

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