Click here to Skip to main content
12,753,195 members (38,781 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0 Gridview
I am having a datagrid view it has 3 columns
Column 1 = ProductId
Column 2 = Price
Column 3 = Quantity

My Question is
How to add quantity by 1 (+1) if the product id and with same price is repeated.
and it should not add the quantity by 1 if the productid is same but price is different..

please give me a small example with it bcoz i am not good in Grids..

Thanks in advanced,
A.Mohammed Owais.
Posted 1-Oct-12 4:55am
Updated 1-Oct-12 4:59am
digimanus 1-Oct-12 10:06am
what have you tried so far?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi Mohammed ,

I have made an example for you to make the functionality you are asking for

here is the design as Screen[^]

then here is the code behind of the event Click of the button Add

//Boolean to check if he has row has been
           bool Found = false;
           if (dataGridView.Rows.Count > 0)

               //Check if the product Id exists with the same Price
               foreach (DataGridViewRow row in dataGridView.Rows)
                   if (Convert.ToString(row.Cells[0].Value) == textBox_ProductId.Text && Convert.ToString(row.Cells[1].Value) == textBox_Price.Text)
                       //Update the Quantity of the found row
                       row.Cells[2].Value = Convert.ToString(1 + Convert.ToInt16(row.Cells[2].Value));
                       Found = true;

               if (!Found)
                   //Add the row to grid view
                   dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1);

               //Add the row to grid view for the first time
               dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1);

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 5 Jun 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100