填充使用的数据源和实体框架编程宣布的datagridview [英] Populating a programatically declared datagridview using datasource and entity framework

查看:198
本文介绍了填充使用的数据源和实体框架编程宣布的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是有使用C#的WinForms DataGridView的一些小问题。这里是我的方案

just having some small issues with c# winforms datagridview. Here's my scenario

我使用实体框架,并正在试图某个实体模型绑定到我的datagridview的数据源。

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();

当我运行这段代码上面的表格类曾在GUI一个DataGridView,它所有工作的罚款。在DataGridView将自动生成的列和行的数。

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.

但是,当我运行外,这个相同的代码,我没有在GUI一个DataGridView,我只是声明它编程,然后设置,如数据源上面的代码。当我做这种方式,不会生成行或列。

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.

什么是这两种不同的datagridviews之间的区别?我知道有表单类的designer.cs文件中设置属性。但我想复制这些设置,它仍然会不填充。

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!

修改

我用的AutoGenerateColumns = true,但它并没有任何区别。另外,我没有真正试图显示此datagridview的,我只是将它绑定到实体对象这样,我可以用一个字符串索引访问它的成员。但我不想查询数据库只是为了让我在一个DataGridView特定格式的信息,因为在我的实际情况,我已经从以前的查询有我的实体数据。我只是用上面的代码,例如,

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.

推荐答案

如果DataGridView的的WinForms只生成列其实际的图形用户界面的一部分。通过坚持我所有的datagridviews的表单设计的形式,在datagridviews生成的列,因为我需要的。我并不真正需要的形式,所以我只是不调用show()方法。好像有点处理这一问题的hackish的方式,但我就是这样解决了这个问题。

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

这篇关于填充使用的数据源和实体框架编程宣布的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆