Avatar of Moti Mashiah
Moti Mashiah
Flag for Canada asked on

C Sharp

Hi Guys ,

I built windows application form and connected to the database through LINQ ADO.NET entity Data Model.

Now I added some Datagridview to my app and I'm trying to write code to show columns from multiple tables but really don't know how to do it.

Can someone give me example how to write this code.

Here is the SQL code with all the tables and columns I need to show in my C# application datagrid.

use RMSmasterDB
select a.ItemLookupCode, a.Description, a.price AS CurrentPrice, CONVERT(DATE,a.DateCreated) AS DateCreated, CONVERT(DATE,a.LastReceived) AS LastRcvd, CONVERT(DATE,a.LastSold) AS LastSold, a.Quantity, QtyRcvd.QtyRcvd, QtySold.QtySold,QtySold.TotalProfit,SoldQ1.SoldQ1, SoldQ2.SoldQ2, SoldQ3.SoldQ3, soldq4.SoldQ4, soldq5.SoldQ5, 

AVG(SoldQ1.SoldQ1 + SoldQ2.SoldQ2 + SoldQ3.SoldQ3 + soldq4.SoldQ4 + soldq5.SoldQ5) AS AVGQuaters, a.ReorderPoint

from dbo.item AS A 

left join (

select  b.ItemID, SUM(b.Quantity) AS QtySold, SUM(b.price * b.quantity) AS TotalProfit
from dbo.TransactionEntry AS B 
group by b.ItemID

           ) AS QtySold on Qtysold.ItemID = a.ID

left join (

select c.ItemID, SUM(c.QuantityReceivedToDate) AS QtyRcvd
from dbo.PurchaseOrderEntry AS C 
group by c.ItemID

           ) AS QtyRcvd on QtyRcvd.itemid = a.ID

left join (

select d.ItemID, ISNULL(SUM(d.quantity),0) AS SoldQ1
from dbo.TransactionEntry AS D
where d.TransactionTime >= dateadd(day,datediff(day,92,GETDATE()),0)
d.TransactionTime <= dateadd(day,datediff(day,0,GETDATE()),0)
group by d.ItemID

          ) SoldQ1 on SoldQ1.ItemID = a.ID

left join (

select e.ItemID, ISNULL(SUM(e.quantity),0) AS SoldQ2
from dbo.TransactionEntry AS e
where e.TransactionTime >= dateadd(day,datediff(day,184,GETDATE()),0)
e.TransactionTime <= dateadd(day,datediff(day,93,GETDATE()),0)
group by e.ItemID

          ) SoldQ2 on SoldQ2.ItemID = a.ID

left join (

select f.ItemID, ISNULL(SUM(f.quantity),0) AS SoldQ3
from dbo.TransactionEntry AS f
where f.TransactionTime >= dateadd(day,datediff(day,276,GETDATE()),0)
f.TransactionTime <= dateadd(day,datediff(day,185,GETDATE()),0)
group by f.ItemID

          ) SoldQ3 on SoldQ3.ItemID = a.ID

left join (

select g.ItemID, ISNULL(SUM(g.quantity),0) AS SoldQ4
from dbo.TransactionEntry AS g
where g.TransactionTime >= dateadd(day,datediff(day,368,GETDATE()),0)
g.TransactionTime <= dateadd(day,datediff(day,277,GETDATE()),0)
group by g.ItemID

          ) SoldQ4 on SoldQ4.ItemID = a.ID

left join (

select H.ItemID, ISNULL(SUM(H.quantity),0) AS SoldQ5
from dbo.TransactionEntry AS H
where H.TransactionTime >= dateadd(day,datediff(day,460,GETDATE()),0)
H.TransactionTime <= dateadd(day,datediff(day,369,GETDATE()),0)
group by H.ItemID

          ) SoldQ5 on SoldQ5.ItemID = a.ID

left join dbo.Department AS Q on q.id = a.DepartmentID

where q.code = '05'

group by a.ItemLookupCode, a.Description, a.price, a.DateCreated, a.LastReceived, a.LastSold, a.Quantity, QtyRcvd.QtyRcvd, QtySold.QtySold,SoldQ1.SoldQ1, SoldQ2.SoldQ2, SoldQ3.SoldQ3, soldq4.SoldQ4, soldq5.SoldQ5, QtySold.TotalProfit, a.ReorderPoint

order by QtySold.TotalProfit DESC

Open in new window


Avatar of undefined
Last Comment
Moti Mashiah

8/22/2022 - Mon

Moti Mashiah

In the example:

I can't get the command  GetData(); when I typed it.
Moti Mashiah

Please can I have some more deeply help I'm really new for this world and the example you provided me is not too clear for me.

I was trying something like that but nothing seem to be working get many red marks.:)

 private void Form1_Load(object sender, EventArgs e)
            var db = new rmsmasterdbtestEntities2();

            dataGridView1.DataSource = 

            EnumerableRowCollection<DataRow> contactQuery = from row in DataSet.tables["


Open in new window

Your help has saved me hundreds of hours of internet surfing.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Moti Mashiah

Thank you very much but I couldn't get it that way and I figured it is going to be a lot of work as I have many tables and columns to add, so I connected to the storage procedure the SQL guy gave me, in this way I could get all the columns I need for the datagrid.

private void Form1_Load(object sender, EventArgs e)
            // TODO: This line of code loads data into the 'rmsmasterdbtestDataSet.SALESANALYSIS' table. You can move, or remove it, as needed.

Open in new window

now it is working
Moti Mashiah