如何删除以前添加的行

时间:2018-02-09 17:56:01

标签: c# .net winforms

我正在开发POS系统,但现在我被困在某个地方,我试图修复3天但是我找不到解决方案

我制作了一个系统,当一个项目缺货时,它不会被添加并显示错误,这个系统对我很好。但是现在我想要做的是,如果数据网格中的数量大于数据库中的项目库存,它将显示错误,告知无法添加。但我找不到自己做的方法。我这样做的想法是,如果数量>库存,显示错误并删除上次添加的行。

这是我的代码

//Brcode system =================================================
    //Brcode system =================================================
    //Brcode system =================================================

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {

        //reading brcode =================================================
        cmd = new SqlCommand("SELECT * FROM Products WHERE Item_Code = @BarCode", con);
        cmd.Parameters.Add(new SqlParameter("@BarCode", Productstxt.Text));
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {

            foreach (DataGridViewRow row in receiptgrid.Rows)
            {                    
                if (Convert.ToInt32(reader.GetInt32(4)) > 1)     //This condition is " If Item stock is avaible add it if not show message box" 
                {
                    prdctname.AppendText(Environment.NewLine + reader.GetString(1));
                    string pname = reader.GetString(1);
                    string price = reader.GetString(2);

                    bool Found = false;

                    if (receiptgrid.Rows.Count > 0)
                    {


                        if (Convert.ToString(row.Cells[0].Value) == reader.GetString(1) && Convert.ToString(row.Cells[1].Value) == reader.GetString(2))
                        {
                            row.Cells[2].Value = Convert.ToString(1 + Convert.ToInt32(row.Cells[2].Value));
                            Found = true;
                        }

                        if (!Found)
                        {
                            receiptgrid.Rows.Add(pname, price, 1, price, indate.Text);
                        }
                    }
                    else
                    {
                        receiptgrid.Rows.Add(pname, price, 1, price);
                    }


                    row.Cells[receiptgrid.Columns["Total"].Index].Value = Convert.ToDouble(row.Cells[receiptgrid.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[receiptgrid.Columns["QTY"].Index].Value);


                    Thread.Sleep(500);
                    prdctname.Text = string.Empty;
                    prdctname.Select(0, 0);
                    prdctname.ScrollToCaret();

                    Productstxt.Select(0, 0);
                    Productstxt.ScrollToCaret();
                    Productstxt.Text = string.Empty;

                }
                else if (Convert.ToInt32(reader.GetInt32(4)) == 1)             // This condition is for "If item stock is out of stock do this"
                {
                    DialogResult result = MessageBox.Show("This item should be out of stock, Continue anyway?", "POS", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (result == DialogResult.Yes)             //This condition is for "If The user did yes then add it"
                    {
                        prdctname.AppendText(Environment.NewLine + reader.GetString(1));
                        string pname = reader.GetString(1);
                        string price = reader.GetString(2);

                        bool Found = false;

                        if (receiptgrid.Rows.Count > 0)
                        {


                            if (Convert.ToString(row.Cells[0].Value) == reader.GetString(1) && Convert.ToString(row.Cells[1].Value) == reader.GetString(2))
                            {
                                row.Cells[2].Value = Convert.ToString(1 + Convert.ToInt32(row.Cells[2].Value));
                                Found = true;

                            }
                            if (!Found)
                            {
                                receiptgrid.Rows.Add(pname, price, 1, price, indate.Text);
                            }
                        }
                        else
                        {
                            receiptgrid.Rows.Add(pname, price, 1, price);
                        }


                        row.Cells[receiptgrid.Columns["Total"].Index].Value = Convert.ToDouble(row.Cells[receiptgrid.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[receiptgrid.Columns["QTY"].Index].Value);


                        Thread.Sleep(500);
                        prdctname.Text = string.Empty;
                        prdctname.Select(0, 0);
                        prdctname.ScrollToCaret();

                        Productstxt.Select(0, 0);
                        Productstxt.ScrollToCaret();
                        Productstxt.Text = string.Empty;
                    }
                }
                else
                {
                    MessageBox.Show("This Item is out of Stock", "POS", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                if (Convert.ToInt32(reader.GetInt32(4)) > Convert.ToInt32(row.Cells[2].Value))
                {
                    MessageBox.Show("The quantity is larger than the stock", "POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
            }
            reader.Close();



            con.Close();
        }

0 个答案:

没有答案