Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two datagridviews(first one called Dgv_Invoice,and 2nd called dgv_costCenter)
the data in the second datagridview(dgv_costCenter) was
item name	price	quantity	value	customerid
keyboard	20	10		 200        1
mouse	        10	10	         100        2  
monitor	        200     2                400        3
keyboard	20      5                100        4
mouse	        10      5                50         5 
keyboard	20      7                140        6
mouse	        10      7                70         7
what i want to do is to show items in the first datagridview(Dgv_Invoice) like that
item name	price	quantity	value	
keyboard	20	22		 440       
mouse	        10	22	         220          
monitor	        200     2                400


What I have tried:

i have create table inside my database called (items_transactions)
and the to solve this problem i made a query to save data from dgv_costCenter
with stored Procedure and then i call the data to show inside Dgv_Invoice grouped by itemname
select itemname,price,sum(quantity)as totalquantity from items_transactions group by itemname,price,quantity . the problem is that i want to do that inside my form without save (dgv_costCenter) into database and call data again to show inside(Dgv_Invoice) with query .. can any one help me.
Posted
Updated 2-Dec-18 1:14am
v2

Maybe this CodeProject article will get you started: OutlookGrid: grouping and arranging items in Outlook style[^]

You can also copy the data into a temporary List<> and then use Linq to group and sum the data, example here: c# - Linq: GroupBy, Sum and Count - Stack Overflow[^]
The List<> can then be used as a DataSource for your DataGridView, see example here: c# - Using a list as a data source for DataGridView - Stack Overflow[^]
 
Share this answer
 
v3
Comments
Abuamer 1-Dec-18 22:04pm    
first of all .. thank you all of you guys. really thank you. but i have tried some way which i want to share with you.i have used a button and made this>>>
private void barbtnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
int sum=0;
for(int i=0;i<Dgv_Invoice.Rows.Count-1;i++)
{
var band=Dgv_Invoice.Rows[i].Cells[0].Value.ToString();
for(int k=0;k<dgv_CostCenter.Rows.Count-1;k++)
{
var band2 = dgv_CostCenter.Rows[k].Cells[0].Value.ToString();
if(band==band2)
{
sum+=Convert.ToInt32(dgv_CostCenter.Rows[k].Cells[2].Value);
Dgv_Invoice.Rows[i].Cells[2].Value=sum;
}
}
}
}

but it works for only first row >
can you help me please
I'm not quite sure if you're talking WinForm, WPF, or ASP.NET/MVC... The answer for the first two is Data Binding. Then you don't have to worry about the control and how to access the data but instead work directly on the data independent of the control.

* For Winforms, here is where to get started: Windows Forms Data Binding | Microsoft Docs[^]
* For WPF: Data Binding (WPF) | Microsoft Docs[^]
 
Share this answer
 
Comments
RickZeeland 1-Dec-18 3:51am    
Data Binding would be the recommended way, but the OP says that he is looking for a way to do it without saving to the database ...
As he mentions DataGridView, it's probably a Winforms application.
Graeme_Grant 1-Dec-18 7:16am    
Depends if he's using a data table linked directly to a DB or separated layers... Totally unclear...
Abuamer 1-Dec-18 16:43pm    
first of all .. thank you all of you guys. really thank you. but i have tried some way which i want to share with you.i have used a button and made this>>>
private void barbtnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
int sum=0;
for(int i=0;i<Dgv_Invoice.Rows.Count-1;i++)
{
var band=Dgv_Invoice.Rows[i].Cells[0].Value.ToString();
for(int k=0;k<dgv_CostCenter.Rows.Count-1;k++)
{
var band2 = dgv_CostCenter.Rows[k].Cells[0].Value.ToString();
if(band==band2)
{
sum+=Convert.ToInt32(dgv_CostCenter.Rows[k].Cells[2].Value);
Dgv_Invoice.Rows[i].Cells[2].Value=sum;
}
}
}
}

but it works for only first row >
can you help me please
In answer to your question to do it another way:
for (int c = 0; c < dgv_CostCenter.Rows.Count - 1; c++)
{
    int sum = 0;
    var costname = dgv_CostCenter.Rows[c].Cells[0].Value.ToString();

    for (int i = 0; i < Dgv_Invoice.Rows.Count - 1; i++)
    {
        var invoicename = Dgv_Invoice.Rows[i].Cells[0].Value.ToString();

        if (costname == invoicename)
        {
            int invoiceValue = Convert.ToInt32(Dgv_Invoice.Rows[i].Cells[2].Value);
            sum += invoiceValue;
            dgv_CostCenter.Rows[c].Cells[2].Value = sum;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900