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

编程入门 行业动态 更新时间:2024-10-26 12:28:38
本文介绍了如果数据已存在,则网格视图添加数量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 datagrid 视图它有3列 第1列= ProductId 第2列=价格 第3栏=数量 我的问题是 如何将数量加1(+1)如果产品ID和相同的价格重复出现。 如果productid相同,它应该将数量加1 但价格不同 .. 请给我一个小例子bcoz我不擅长网格.. 谢谢你提前, A.Mohammed Owais。

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.

推荐答案

嗨穆罕默德, 我已经为你做了一个例子来制作你要求的功能 这里设计为Screen i.imgur/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 i.imgur/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); }

更多推荐

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

本文发布于:2023-10-19 18:17:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   视图   数量   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!