如何在datagridview C#winforms中显示来自多个表的数据 [英] how do I show data from multiple tables in datagridview C# winforms

查看:83
本文介绍了如何在datagridview C#winforms中显示来自多个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在为燃油泵开发自动化手动系统。到目前为止,我已经完成了我的任务,但现在我被困在与从多个相关表中检索数据相关的内容到一个数据网格视图中。我已经'谷歌'我的问题,但未能找到正确的方法继续前进。这是我的场景说明:



- 我的表名为'部门',其中'车辆'表; (1 - *)关系。

- 然后'车辆'表有'月'表保持每月结算明细; (1 - *)关系。

- 和'Months'表有两个表'Fuel_Bill'和< b>'Lubi_Bill'保留账单的最高级别信息。(两者都与'月'表有 1-1 关系)。



我使用SQL LocalDB上的实体框架设计师第一个模型构建它。



所以,现在我是坚持展示:

- 部门的每月抽象视图,其中包含与车辆相关的详细信息以及在一个数据网格视图中所选月份的燃料_bill详细信息。



这是一个很长的问题,但要明确我提供这些细节,请帮助我,如果我无法实现这一点,我的整个项目都会失败。 />


任何帮助将不胜感激。

提前致谢。



我' m添加图片链接以便更好地理解



实体框架模型

实体框架模型设计



我想要实现的目标

我的目标说明

Hi everyone,
I'm developing an automation of manual system for a fuel pump. Up till now I've accomplished my tasks but now I'm stuck on something related to retrieve data from multiple related tables into one data grid view. I've already 'Google' my problem but failed to find the right way to move on. Here is my scenario Explanation:

- I've table named 'Department' which have 'Vehicles' table; (1-*) relation.
- then 'Vehicles' table have 'Months' table to keep monthly billing details; (1-*) relation.
- and 'Months' table have tow tables 'Fuel_Bill' and 'Lubi_Bill' to keep the top level information of bills.(Both have 1-1 relation with 'Months' table).

I've build this using entity-framework designer first model on SQL LocalDB.

So, now I'm stuck on showing:
-Department's monthly abstract view which have the details related to 'Vehicles' and the 'Fuel _bill" details of selected 'Month' in one data grid view.

It's a long question but to be clear I give these details, Please help me out, my whole project will fail if I can't accomplish this.

Any help will be appreciated.
Thanks in advance.

I'm adding links to pictures for better understanding

Entity Framework Model
Entity Framework Model design

What I'm trying to achive
My Goal discription

推荐答案







现在你有桌子:车辆,月份,Fuel_Bill,Lubi_Bill



我有一个最好的主意。



现在这些表格是相关的让我考虑车辆表有各种车辆的集合。并且每辆车都有一个唯一的ID,这是主键。

其次生病给你一个例子



车辆表 - 列: VehicleID,Name,Model,Year .... etc

月份--- MonthID,月份int(10),年份int(10)

fuel_bill-- FuelID, VehicleID,MOnthID,....等


现在你要做的是......



Hi,


Now You have Table : Vehicles, Months, Fuel_Bill, Lubi_Bill

I have one best idea to do this.

Now these tables are related let me consider Vehicles Table have the Collection of various Vehicles. and each vehicle will have a unique id which is the primary key.
secondly ill give u an example

vehicle table--Columns: VehicleID, Name, Model, Year....etc
Months--- MonthID, Month int(10), year int (10)
fuel_bill-- FuelID, VehicleID, MOnthID, ....etc

now what you are going to do is..

select * from fuel_bill FB 
left outer join Vehicles V on V.VehicleID = FB.VehicleID
left outer join months m oh m.monthid = fb.monthid
--to filter specific month and year
where m.month = '1' and m.year = '2014'
--now ull get all the record for that specific period.



这只是一个用ur数据库修改它的例子

现在获取这个数据源并将其绑定到datagridview。



快乐编程。


this is just an example modify it with ur database
Now get this datasource and bind it to the datagridview.

Happy Programming.


简单的方法是:



创建视图然后将其绑定到gridview。



另见:

如何将SQL Server视图与实体框架一起使用 [ ^ ]
The simple way is:

To create a view then Binding that to your gridview.

Also See:
How to use SQL Server Views with the Entity Framework[^]


var viewModels = context.Months
    .Where(m => m.Month_Name == selectedMonthName &&
        m.Vehical.Dept_Id == selectedDepartmentId)
    .Select(m => new
    {
        Vehc_Number = m.Vehical.Vehc_Number,
        FB_Id = m.Fule_Bill.FB_Id,
        LB_GTAmount = m.Fule_Bill.LB_GTAmount,
        LB_GTQuantity = m.Fule_Bill.LB_GTQuantity
    }
    .ToList();





所以这就是我想要的解决方案感谢Slauma。


这篇关于如何在datagridview C#winforms中显示来自多个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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