LINQ to Entities和填充DataGrid [英] LINQ to Entities and filling a DataGrid

查看:54
本文介绍了LINQ to Entities和填充DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前已经问过这个问题,但我得到了一个答案,虽然不是我想要的并且没有使用DataGridView,但仍然有效,所以感谢Paul Johnson。但是,我现在需要得到一个使用DataGridView的答案,因为我想使用DataGridView的所有酷炫功能,而且更优雅。


场景如下遵循...我希望使用LINQ to Entities从C#程序中选择性地读取SQL数据库中的大量记录,例如,名称为Smith的每个人。我现在想要使用所有Smith的Data来填充DataGridView。数据,即名字,地址详细信息等。现在因为史密斯这么常见的名字可能有几十条记录,而DataGridView让我有机会滚动列表并选择我想要的那个。


我确定可以做到;我只是不知道怎么......任何人?

I have asked this question previously and I had an answer that, while not exactly what I wanted and did not use a DataGridView, worked, so for that thanks Paul Johnson. However, I now need to get an answer that uses the DataGridView, since I would like to use all the cool features of the DataGridView, and also be more elegant.

The scenario is as follows... I wish to selectively read a number of records from a SQL database from a C# program using LINQ to Entities, e.g., everybody whose name is Smith. I now want to populate a DataGridView with all the "Smith''s" data, i.e., first name, address details etc. Now because Smith is such a common name there could be dozens of records, and the DataGridView gives me the opportunity to scroll through the list and select the one I want.

I''m sure it can be done; I just don''t know how...Anyone?

推荐答案

将你的DataGridView绑定到一个继承自BindingList< T>的类。或者ObservableCollection< T>,如果要修改其上的项目并监听数据源中的更改,请实现INotifyPropertyChange和INotifyCollectionChange。您可以轻松地将它们与Linq一起使用:ObservableCollection有一个构造函数,可以将Linq查询作为参数:
Bind your DataGridView to a class that inherits from BindingList<T> or ObservableCollection<T>, and if you want to modify items on it and listen to changes in your data source, implement INotifyPropertyChange and INotifyCollectionChange. You can easly use them with Linq: ObservableCollection has a constructor that can take Linq queries as a parameter:
展开 | 选择 | Wrap | 行号


谢谢Arie ,现在才刚刚看到你的建议......长篇大论......有约束力的工作和许多,非常感谢。你可以告诉我如何限制显示的列数,在数据库中我想要显示的只有四个或五个,出于美观和空间。
Thanks Arie, only just seen your advice now... long story... binding worked and for that many, many thanks. Could you advise me on how I can restrict the number of columns that are displayed, of the twenty or so in the database I want to show just four or five, for aesthetic reasons as well as space.


有一个属性DataGridView.AutoGenerateColumns。将其设置为false因此,您可以将自己需要的列添加到DataGridView.Columns集合中。

There is a property DataGridView.AutoGenerateColumns. Set it to "false" so you can add the columns you need yourself to DataGridView.Columns collection.

展开 | 选择 | Wrap < span class =codeDivider> | 行号


这篇关于LINQ to Entities和填充DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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