在DataGridView C#上为现有项添加数量

时间:2016-09-11 20:32:15

标签: c# mysql datagridview

我在POS系统上工作,我的问题是当我在数据网格上添加现有项目时,它只会添加相同的项目,例如

这是产品信息:

Item Info

单击“确定”时的输出:

Output 这是我的添加代码:

private void add_Click(object sender, EventArgs e)
    {
        DateTime d = DateTime.Now.Date;

        if (Buy.Text == "")
        {
            MessageBox.Show("Quantity required", "ADD", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        else
        {

            try
            {

                conn = koneksyon.getConnect();
                conn.Open();

                cmd = new SqlCommand("select PurchaseNo from Purchase where CustomerNO = '" + CustomerID.Text + "'", conn);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    label18.Text = dr[0].ToString();
                }
                if (label18.Text == OR.Text)
                {

                    conn = koneksyon.getConnect();
                    conn.Open();
                    cmd = new SqlCommand("delete from Purchase where PurchaseNo ='" + dataGridView1.CurrentRow.Cells[6].Value + "'And CustomerNO ='" + CustomerID.Text + "'", conn);
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    conn.Close();
                    label18.ResetText();

                }
                dr.Dispose();
                int t = 0;

                conn = koneksyon.getConnect();
                conn.Open();

                string fullname = fName.Text + " " + lName.Text;

                cmd = new SqlCommand("insert into Purchase(PurchaseNo, CustomerNO,StaffID) values ('"
                    + OR.Text + "','"
                    + CustomerID.Text + "','"
                    + txtStaffID.Text + "')", conn);
                cmd.ExecuteNonQuery();

                cmd = new SqlCommand("insert into Details(PurchaseNo, ProductID, Quantity, Price, Subtotal) values ('"
                    + OR.Text + "','"
                    + itemID.Text + "','"
                    + Buy.Text + "','"
                    + Price.Text + "','"
                    + Subtotal.Text + "')", conn);
                cmd.ExecuteNonQuery();

                MessageBox.Show("Item Added");
                cmd = new SqlCommand("select Qty from Product where ProductID ='" + itemID.Text + "'", conn);
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    t = Convert.ToInt32(itemQty.Text) - Convert.ToInt32(Buy.Text);

                }
                dr.Dispose();

                cmd = new SqlCommand("UPDATE Product set Qty ='" + t + "' where ProductID ='" + itemID.Text + "'", conn);
                cmd.ExecuteReader();

                dr.Dispose();

                conn = koneksyon.getConnect();
                conn.Open();
                dataGridView1.Rows.Clear();
                dataGridView1.Visible = true;


                //cmd = new SqlCommand("Select a.ProductID, a.Description, a.Qty, b.Price, b.SupplierID from Product as a inner join Inventory as b on a.ProductID = b.ProductID", con);


                cmd = new SqlCommand("Select b.ProductID,c.Description, b.Quantity,b.Price, b.Subtotal , a.CustomerNO, b.PurchaseNo from Details as b inner join Purchase as a  on b.PurchaseNo= a.PurchaseNo join Product c on c.ProductID = b.ProductID where CustomerNO ='" + CustomerID.Text + "'", conn);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {

                    dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6]);
                }
                dr.Dispose();

                //cmd = new SqlCommand("Update Purchase Set PurchaseNo ='" + OR.Text + "', CustomerNO ='" + CustomerID.Text + "'", conn);
                //cmd.ExecuteNonQuery();

                itemID.Clear();
                Price.Clear();
                itemDesc.Clear();
                itemQty.Clear();
                Subtotal.Clear();
                Buy.Enabled = false;
                Buy.Clear();
                itemCode.Clear();
                itemCode.Focus();
                button2.Enabled = true;
            }
            catch (Exception)
            {

            }

        }

    }

现在我的问题是,当我添加另一个相同的项目时,它将显示如下:

Another Item when add

,我想显示它像这样,但我不知道该怎么做:

enter image description here

0 个答案:

没有答案