Click here to Skip to main content
12,251,027 members (40,880 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 3:55am
Edited 1-Oct-12 3:59am
v2
Comments
digimanus 1-Oct-12 10:06am
   
what have you tried so far?

1 solution

Rate this: bad
 
good
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
http://i.imgur.com/yc4s5.jpg[^]

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);
               }
 
           }
           else
           {
               //Add the row to grid view for the first time
               dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1);
           }
  Permalink  

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
Web01 | 2.8.160426.1 | Last Updated 5 Jun 2014
Copyright © CodeProject, 1999-2016
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