CaptivativeMortal
CaptivativeMortal

Reputation: 3

Data from multiple tables into datagridview C# winforms

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 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 of my data base structure.

Entity Framework model:

Model

And what I'm trying to achieve:

View

Upvotes: 0

Views: 2230

Answers (1)

Slauma
Slauma

Reputation: 177133

I would start from context.Months to collect the required data for your grid:

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

viewModels will be a list of anonymous objects with those four properties that you want to show in the grid. It should be possible to bind this list to the grid view.

Upvotes: 1

Related Questions