DataGridView使用LINQ安装程序不显示任何内容-为什么? [英] DataGridView not showing anything using LINQ setup - why?

查看:146
本文介绍了DataGridView使用LINQ安装程序不显示任何内容-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

在我当前的项目中,我使用VS2008和c#将LINQ to SQL与SQLite数据库一起使用.我想要一个表单来显示表的内容,因此设置一个DataGridView来做到这一点.不幸的是,我无法显示任何内容-DGV保持空白.

我看过的所有文章都建议执行以下操作,尽管也可以使用BindingSource进行以下操作:

Hi Folks,

In my current project I''m using LINQ to SQL with an SQLite database, using VS2008 and c#. I wanted a form to display the contents of a table, so set up a DataGridView to do that. Unfortunately I can''t get anything displayed - the DGV remains blank.

All the articles I''ve seen suggest the following should work, though it can also be done with a BindingSource:

db = new RWInfoDB(dbConnection);
var rwInfoTableQuery = from d in db.GetTable<RWInfoTable>() orderby d.Id select d;
pkgView.Show();
int c = rwInfoTableQuery.Count();
pkgView.dataGrid.DataSource = rwInfoTableQuery;



其中RWInfoDB是我的DataContext派生的类,dbConnection返回一个预装的SQLite连接对象,pkgView是DGV处于打开状态的形式,并且"int c"行仅证明该查询有效.
在运行时检查DGV对象将显示具有可见数据的DataSource成员-我可以看到其中的行-但是DGV的列和行"中为0-好像它甚至都没有查看DataSource.

[我从设置数据源后调用Show()开始,但是将其移动以查看是否有帮助]

DGV中是否有一些属性可以阻止它以这种方式工作?

有什么想法吗?

Ruth



Where RWInfoDB is my DataContext-derived class, dbConnection returns a pre-canned SQLite connection object, pkgView is the form the DGV is on, and the "int c" line just proves the query works.

Inspecting the DGV object at runtime shows a DataSource member with visible data - I can see the rows in there - and yet the DGV has 0 in Columns and Rows - as if it hasn''t even looked at the DataSource.

[I started with Show() called after setting DataSource, but moved it to see if that would help]

Are there properties in the DGV that could stop it working in this way?

Any thoughts?

Ruth

推荐答案

要在LINQ中读取表,您可以使用多种选择.

最简单的方法是将表拖入DBML设计并使用以下代码:

To read a table in LINQ you have a variety of options.

The simplest would be to drag the table into the DBML design and use the following code:

Using (Mydatacontext mydc = new Mydatacontext ())
{
    List<mytable> rows = mydc.mytableresults.tolist();
    mydatasource= rows;
}
</mytable>


另一个选择是创建一个存储过程并以与上述相同的方式读取结果以填充网格.当查询变得更加复杂时,此方法会更好地工作


Another option would be to create a stored procedure and read the results in the same way as above to populate your grid. this method works much better when your queries become more complex


对其进行排序!

我在MS docs中发现必须使用属性"的废弃"注释-它没有说什么.

但是,尽管LINQ中的一些事情对于我的表行对象的基于字段的类(在其中包含[Column]的标记为[Table]的类)都可以正常工作,但是DGV却没有.为所有字段设置自动属性,然后DGV起作用.
Sorted it!

I discovered in the MS docs a "throwaway" comment that "you must use properties" - what for, it didn''t say.

However, although several things in LINQ worked fine with a field-based class for my table row object (the one tagged [Table] with [Column]''s within it), DGV doesn''t. Make automatic properties of all the fields and the DGV works.


这篇关于DataGridView使用LINQ安装程序不显示任何内容-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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