Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
How to sum of two rows values inside the DataGridView, if the id of the both rows same and create one rows instead of two rows in the DataGrid?

Like the following example:
id           quantity       Bonus        price           total

01             25           10           123            4305

01             5             10          123            1845



I want to sum of the above two rows like this in the DataGrid.
id           quantity        Bonus         price          total

01              30            20             123          6150



if i take loop on it then duplicate value insert in the datagridview.

like my code

For example:

for(int i=0; i<datagridview.rows.count; i++)
{

if(datagridview.rows[i].cells[0].value==txtid.Text)
{
if(comboboxtype=="Stock")
{
//For Stock
///updation code,if value exist in the datagrid
}
else
{
//For Bonus
///updation code,if value exist in the datagrid
}

}
else
{
if(comboboxtype=="Stock")
{
//For Stock

/// ///Insertion code, if value does not exist in the datagrid
}
else
{
/// For Bonus
/// ///Insertion code, if value does not exist in the datagrid
}


}
}



Please,Anyone Can help me?
Posted
Updated 13-Jul-20 12:22pm
v4
Comments
Thanks7872 13-Sep-13 8:04am    
Post the code in order to show the effort you have made to achieve this.

I would suggest to use LINQ Query to assign datasource to the grid as mentioned below :

For Example My class name is stock then

STOCK Class:
C#
public class Stock
    {
        public int Id { get; set; }

        public int Quantity { get; set; }

        public int Bonus { get; set; }

        public int Price { get; set; }

        public int Total { get; set; }
    }


Then below linq query should be used to assign datasource :

C#
var stocks = stockList.Select(s => s.Id).Distinct().Select(stock => new Stock
                {
                    Id = stock,
                    Quantity = stockList.Where(s => s.Id == stock).Sum(s => s.Quantity),
                    Bonus = stockList.Where(s => s.Id == stock).Sum(s => s.Bonus),
                    Price = stockList.Where(s => s.Id == stock).Select(s => s.Price).FirstOrDefault(),
                    Total = stockList.Where(s => s.Id == stock).Sum(s => s.Total),
                }).ToList();


Note : Above query will sum all the properties except ID and price. and stockList is the list object of Stock which contains all the data for Stock.
 
Share this answer
 
v3
Comments
Zain -Ul- Arifeen 13-Sep-13 10:15am    
if i take loop on it then duplicate value insert in the datagridview.

like my code

For example:

for(int i=0; i
use linq query on your dataset with sum function and group by clause...

like .....

var rows = from a in query
group a by new {
h = a.c.BookingRefex,
b = a.c.ClientRefex,
c = a.b.PickupCity,
d = a.b.PickupPostalCode
} into g
select new {
BookingRefex = g.Key.h,
ClientRefex = g.Key.b,
SumQuantity = g.Sum(p => p.bg.Quantity),
Value = g.First().c.Value
}
 
Share this answer
 
I suggest you this solution:

before inserting a new line to the datagridview, check if the ID is already inserted to the datagridview, so if it inserted before -> do this:

C#
private void button1_Click(object sender, EventArgs e)
        {
            bool theItemIsExist = false;

            // this (for) loop is to check if the id is already exist .. if true sum new values to the old values
            for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString() == TXTID.Text)
                {
                    dataGridView1.Rows[i].Cells[1].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value) + Convert.ToDecimal(TXTQuantity.Text);
                    dataGridView1.Rows[i].Cells[2].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value) + Convert.ToDecimal(TXTBonus.Text);
                    dataGridView1.Rows[i].Cells[3].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value) + Convert.ToDecimal(TXTPrice.Text);
                    dataGridView1.Rows[i].Cells[4].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value) + Convert.ToDecimal(TXTTotal.Text);

                    theItemIsExist = true;
                    break;
                }
            }

            // after checking the list and there is no id exist .. then add new line with values
            if (!theItemIsExist)
            {
                dataGridView1.Rows.Add(TXTID.Text, TXTQuantity.Text, TXTBonus.Text, TXTPrice.Text, TXTTotal.Text);
            }
        }


I tested it , and it works

note: do some changes in this code to fit with yours (names of tools and so on)
 
Share this answer
 
v2

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