如何将数据库绑定到 WPF 中的 ListView?
我有一些想要显示的数据(大约 1000 行)。我希望用户能够动态过滤和排序数据。我还必须能够保存和加载数据。我对数据库相对陌生,需要您的帮助来决定哪种方法是最好/最合理的:
第一个变体: 这是我目前的做法。我正在使用绑定到 RowItems 的 ObservableCollection 的 ListView/GridView。我现在可以使用视图进行排序和过滤。保存和加载是通过序列化完成的。
第二个变体: 使用数据库来存储数据。然后,数据被加载到与上面相同的业务对象中。
第三种变体: 将 ListView 直接绑定到数据库。我刚刚发现这在某种程度上是可能的,但对细节仍然有些模糊。特别是在排序和过滤方面(可以通过数据库查询来完成吗?)
第四种变体: 将数据存储在数据库中。使用bussines对象的ObservableCollection进行绑定。但是通过 SQL 查询进行过滤(和排序?)。
这些是我必须满足我的要求的想法。我想知道哪一个是最好的/最简单的/执行得最好的/等等。或者如果您建议其他方法。在此处输入代码
I have some data (about 1000 rows) that I want to display. I want the user to be able to filter and sort the data dynamicly. Also I have to be able to save and load the data. I am relativly new to databases and need your help deciding which approach is the best/most resonable:
1st variant:
This is my current approach. I am using a ListView/GridView that is bound to a ObservableCollection of RowItems. I can now use the View to sort and filter. Saving and loading is done via Serialisation.
2nd variant:
Use of a database to store the data. The data is then loaded into the same business objects as above.
3rd variant:
Bind the ListView directly to a database. I just found out that this is somehow possible and am still somewhat hazy on the details. Especially on the aspect of sorting and filtering (Could that be done via a database Query?)
4th variant:
Store data in database. Use ObservableCollection of bussines objects for binding. But filter (and sort?) via SQL query.
These are the ideas I have to fullfilling my requirements. I'd like to know which one is the best/easiest/best performed/etc. or if you suggest an other approach.enter code here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Peter,您需要考虑以下几点:
尽可能不要在后端数据和显示之间进行紧密集成。使以后更容易更改。您可以使用 NHibernate 或其他 ORM 来更轻松地表示和从数据库读取/写入数据。这样,它们之间就有了层/层(也称为 n 层)
你只需要调用数据库来读取数据,它是1000行,这还不错,你可以一次性读取它们并将其保存在记忆中。您可以在内存中的 DomainModel/Buisness 对象上执行任何过滤/排序/分组,而无需接触库。
我会使用变体 1) 和 2) 的混合物。
我将使用业务对象来表示数据库中的数据(NHibernate 非常方便)。然后,我将在客户端上执行所有数据过滤/排序,而无需每次都请求数据库。对数据库的任何写入都必须经过某种领域模型层或 ORM,以便它们不会紧密链接。这允许我在不影响 GUI 前端的情况下更改数据库
Peter, there are couple of points you need to consider:
Where possible never make tight integration between back end data and display. Makes it easier to change either later on. you can use NHibernate or other ORM to make it easier to represent and read/write data from database. That way you have layer/tier in between them (aka n-tier)
you will only need to make call to DB for reading the data, it is 1000 rows it is not too bad, you can read them in one go and keep it in memory. Any filtering/sorting/grouping that you can do it on the in memory DomainModel/Buisness Object without touching the library.
i would use mixture of variant 1) and 2).
I would use business object to represent your data from Database (NHibernate is quite handy). I would then perform all data filtering/sorting on the client without requering DB each time. Any writes to the database must go through some sort of Domain Model layer or ORM so that they are not tighly linked. That allows to me to chnage DB without effecting the GUI Front end