Click here to Skip to main content
16,008,490 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Updated 1-Oct-12 3:59am
v2
Comments
Herman<T>.Instance 1-Oct-12 10:06am    
what have you tried so far?

1 solution

Hi Mohammed ,

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

here is the design as Screen
http://i.imgur.com/yc4s5.jpg[^]

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

C#
//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);
               }

           }
           else
           {
               //Add the row to grid view for the first time
               dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1);
           }
 
Share this answer
 
Comments
Kibet Chirchir 28-Jul-24 16:42pm    
How do i Check Existing Rows and Increase Value in DataGridview if the item already exist in C#??

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