将已添加的数据网格值与新网格值进行比较

时间:2017-07-13 17:09:38

标签: c# .net

嘿伙计,所以我遇到了一个小问题,我从一个从数据库中读取的文本框向我的gridview添加行,行正在成功添加,但我想验证是否添加了相同的行,以便数量列增加1,所以这是我的代码:

            if (e.KeyChar == (char)13)
            {
                SqlCommand cmd = new SqlCommand("select medicine_name,medicine_sellingPrice from Medicine where medicine_barcode = '" + txtBarcode.Text + "'", con);

                try
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())
                    {
                          dataGridView1.Rows.Add(new object[] { reader["medicine_name"].ToString(), reader["medicine_sellingPrice"].ToString(), z });

                          //Calculations
                          int sum = 0;
                          int sum1 = 0;
                          for (int i = 0; i < dataGridView1.Rows.Count; i++)
                          {
                              sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
                              sum1 = sum * 1500;
                          }
                          toolDollar.Text = "Cost In Dollar: " + sum.ToString() + " " + "$";
                          toolLBP.Text = "Cost in LBP: " + sum1.ToString() + " " + "LBP";
                          toolCount.Text = "Total Items:  " + dataGridView1.RowCount.ToString();                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    con.Close();
                }
            }

每次我添加相同的行时,我希望我的Z值在已添加的行上增加而不添加新行。

提前感谢。

0 个答案:

没有答案