如果数据已存在,则网格视图添加数量。 [英] Grid View add quantity if data already exists..
本文介绍了如果数据已存在,则网格视图添加数量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 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
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);
}
这篇关于如果数据已存在,则网格视图添加数量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文