在datagridview中添加新行之前检查文本框

时间:2016-08-18 06:58:14

标签: c# datagridview

我想检查文本框中的数量以及row.Cells[2].Value中的datagrigview1。 如果它小于数据库中的stock列,则添加一个新行。 请帮忙

if (DB_Reader.HasRows)
{
    //Boolean to check if he has row has been
    bool Found = false;
    if (dataGridView1.Rows.Count > 0)
    {
        //Check if the product Id exists
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToString(row.Cells[0].Value) == textBox1.Text && txtqty+Convert.ToInt16(row.Cells[2].Value) <= Convert.ToInt32(DB_Reader.GetString("stock")))
            {
                //Update the Quantity of the found row
                row.Cells[2].Value = Convert.ToString(txtqty + Convert.ToInt16(row.Cells[2].Value));
                row.Cells[3].Value = Convert.ToInt16(row.Cells[2].Value) * Convert.ToDecimal(DB_Reader.GetString("sale_price"));
                Found = true;
                this.textBox1.Text = "";
                this.textEdit4.Text = "1";
            }
        }
        if (!Found && txtqty <= Convert.ToInt32(DB_Reader.GetString("stock")))
        {
            //Add the row to grid view
            dataGridView1.Rows.Add(DB_Reader.GetString("product_barcode"), DB_Reader.GetString("product_name"), txtqty, txtSum);
            this.textBox1.Text = "";
            this.textEdit4.Text = "1";
        }
    }
    else
    {
        if (txtqty <= Convert.ToInt32(DB_Reader.GetString("stock")))
        {
            //Add the row to grid view for the first time
            dataGridView1.Rows.Add(DB_Reader.GetString("product_barcode"), DB_Reader.GetString("product_name"), txtqty, txtSum);
           this.textBox1.Text = "";
           this.textEdit4.Text = "1";
        }
    }
}

0 个答案:

没有答案