且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果数据已存在,则网格视图添加数量。

更新时间:2023-10-19 18:53:34

嗨***,



我已经为你做了一个例子来制作你要求的功能



这里设计为Screen

http://i.imgur.com/yc4s5.jpg [ ^ ]



然后在这里是代码beh ind of the event点击按钮添加



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);
           }