Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag 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 = 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 Ganapathi
Flag of India image

Avatar of Moti Mashiah


In the example:

I can't get the command  GetData(); when I typed it.
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

Avatar of Ganapathi
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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