如果产品ID存在于当前datagridview
中,则将数量增加一(+1)我的datagridview有9列:
Column 1 = Edit_Checkbox Column 2 = ItemCount
Column 3 = DGV_PRODUCT_ID Column 4 = DGV_PRODUCT_DESC
Column 5 = DGV_UNIT_PRICE Column 6 = DGV_QUANTITY
Column 7 = DGV_DISCOUNT Column 8 = DGV_TOTAL_PRICE
Column 9 = DGV_NOTES
如果产品ID存在于当前datagridview
中,我试图将数量增加一(+1)我的下面的代码工作正常,但我有一个问题,即当它存在相同的产品ID时 如果进程按顺序完成,则将Quantity增加1,但如果我输入了产品ID 以非顺序方式,它不会将数量增加1,而是在datagridview中插入新行。
例如:
输入产品ID编号1003 2项目数量将为2
输入产品ID编号3000 1项目数量将为1
输入产品ID编号1003当产品已存在于datagridview中时,1项目数量仍为1。我该如何避免这种情况?
private void SelectedProductData()
{
int ItemCount = DGV_INVOICE.Rows.Count;
bool ProductIDExist = false;
string connstr = @"Data Source=orcl; User Id=user; password=pwd;";
string cmdtxt = @"SELECT PRODUCT_ID,
PRODUCT_DESC,
UNIT_PRICE
FROM WAREHOUSE
WHERE PRODUCT_ID = :P_Product_ID";
try
{
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
{
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdtxt;
cmd.Parameters.Add(new OracleParameter(":P_Product_ID", OracleDbType.Varchar2)).Value = TB_Product_ID.Text;
OracleDataReader oraReader = cmd.ExecuteReader();
while (oraReader.Read())
{
ItemCount++;
RowCountLabel.Text = ItemCount.ToString();
DataGridViewRow dgvRow = new DataGridViewRow();
foreach (DataGridViewRow ItemRow in DGV_INVOICE.Rows)
{
if (Convert.ToString(ItemRow.Cells[2].Value) == TB_Product_ID.Text)
{
MessageBox.Show(Convert.ToString(ItemRow.Cells[2].Value) + " 1 " + TB_Product_ID.Text);
ProductIDExist = true;
break;
}
}
if (ProductIDExist)
{
MessageBox.Show("2");
//dgvRow.Cells[5].Value = Convert.ToString(1 + Convert.ToInt64(dgvRow.Cells[5].Value));
DGV_INVOICE.Rows[dgvRow.Index].Cells[5].Value = Convert.ToString(1 + Convert.ToInt64(dgvRow.Cells[5].Value));
}
else
{
MessageBox.Show("3");
//Add the row to grid view for the first time
dgvRow.Cells.Add(new DataGridViewCheckBoxCell()); //Edit_Checkbox index 0
dgvRow.Cells[0].Value = false;
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //ItemCount index 1
dgvRow.Cells[1].Value = ItemCount;
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_PRODUCT_ID index 2
dgvRow.Cells[2].Value = oraReader.GetValue(0);
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_PRODUCT_DESC index 3
dgvRow.Cells[3].Value = oraReader.GetString(1);
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_UNIT_PRICE index 4
dgvRow.Cells[4].Value = oraReader.GetValue(2);
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_QUANTITY index 5
dgvRow.Cells[5].Value = "1";
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_DISCOUNT index 6
dgvRow.Cells[6].Value = "0";
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_TOTAL_PRICE index 7
//dgvRow.Cells[7].Value = "0";
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_NOTES index 8
//dgvRow.Cells[8].Value = "-";
DGV_INVOICE.Rows.Add(dgvRow);
dgvRow.Selected = true;
}
}
}
}
catch (Exception EX)
{
MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
TB_ORDER_NOTE.Text = EX.Message; /* Testing Purpose */
}
}
答案 0 :(得分:0)
相反,请尝试
bool ProductIDExist = false;
foreach (DataGridViewRow ItemRow in DGV_INVOICE.Rows)
{
//Check if the product Id exists with the same Price
ProductIDExist |= Convert.ToString(ItemRow.Cells[1].Value) == TB_Product_ID.Text
if (productIDExist)
{
break;
}
}
if (productIDExist)
{
//increase quantity by 1
}
else
{
//add a new row
}