如何在C#中的datagridview中在运行时更改价格

时间:2018-03-28 16:26:41

标签: c# .net sql-server

我有两个文本框。一个用于itemNo,另一个用于quantity

quantity文本框中,我使用了输入键keypressed事件。

当我按数量输入时,datagridview从dgv中提取值并在列表中显示。

我的问题是如何在运行时更改价格?

我试过但没有成功。

这用于从数据库中获取商品价格并在数据网格视图中显示。

//here it is
public void enterToGrid()
{
    try
    {
        receipt obj = new receipt();
        bool found = false;
        string txt = "select * from products where qty>='" + tb_qty.Text + "' and id='" + tb_ino.Text + "'";
        SqlCommand command = new SqlCommand(txt, con);
        con.Open();
        SqlDataReader r = command.ExecuteReader();
        while (r.Read())
        {
            int price = int.Parse(tb_qty.Text.ToString()) * int.Parse(r[4].ToString());
            totalprice = totalprice + price;
            int tPrice = price;
            int p = int.Parse(r[4].ToString());
            int iCode = int.Parse(tb_ino.Text.ToString());
            int q = int.Parse(tb_qty.Text.ToString());
            MessageBox.Show("item Code: " + iCode.ToString() + " Name : " + r[2].ToString() + " price : " + p.ToString() + " qty : " + q.ToString());
            if (dataGridView1.Rows.Count > 0)
            {
                //Check if the product Id exists with the same Price
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (Convert.ToString(row.Cells[1].Value) == tb_ino.Text)
                    {

                        row.Cells[3].Value = Convert.ToString(Convert.ToInt32(tb_qty.Text) + Convert.ToInt32(row.Cells[3].Value));
                        row.Cells[5].Value = (Convert.ToInt32(row.Cells[3].Value) * Convert.ToInt32(row.Cells[4].Value)).ToString();
                        found = true;
                    }
                }
                if (!found)
                {                       
                    receipt rr = new receipt() { id = dataGridView1.RowCount, itemCode = Convert.ToInt32(tb_ino.Text), name = r[1].ToString(), quantity = Convert.ToInt32(tb_qty.Text), price = Convert.ToInt32(r[4]), total = ((Convert.ToInt32(tb_qty.Text) * (Convert.ToInt32(r[4])))) };
                    receiptBindingSource.Add(rr);
                    receiptBindingSource.MoveLast();
                }
            }
        }

        lbl_ti.Text = " " + (dataGridView1.RowCount - 1) + " ";
        lbl_total.Text = " " + totalprice + " ";
        con.Close();
    }
    catch (Exception ee)
    {
        MessageBox.Show(ee.Message, "Error from database");
    }
    finally
    {
        con.Close();
    }
}

0 个答案:

没有答案