使用c#在运行时向datagridview添加新行

时间:2015-02-10 17:10:37

标签: c# winforms datagridview

我正在尝试在textbox leave事件的datagridview中添加行。每当我离开文本框时,与文本框中输入的文本相对应的值应逐个添加到网格中。但在我的情况下,当我将文本框留在第二个条目时,网格中较早添加的第一行消失,仅添加第二行。以下是代码。任何人都可以指导我在这里犯的错误吗?

    private void textBox4_Leave(object sender, EventArgs e)
    {

        //textBox1.Focus();
        int rcount = dataGridView1.RowCount;
        if (rcount == 1)
        {
            dataGridView1.Columns.Clear();
        }
        DataTable dt = new DataTable();
        decimal cost = 0;
        decimal retail = 0;
        decimal discount = Convert.ToDecimal(textBox4.Text);
        string bilno = lblBil.Text;
        MySqlConnection connection = new MySqlConnection(myconnectionstring);
        connection.Open();
        string money = "SELECT COST_PRICE,RETAIL_PRICE FROM INVENTORY_VALUE WHERE BARCODE = @barcode";
        MySqlCommand cmd1 = new MySqlCommand(money, connection);
        cmd1.Parameters.AddWithValue("@barcode", textBox2.Text);
        MySqlDataReader myreader = cmd1.ExecuteReader();
        while (myreader.Read())
        {
            cost = decimal.Parse(myreader["COST_PRICE"].ToString());
            retail = decimal.Parse(myreader["RETAIL_PRICE"].ToString());
        }
        connection.Close();
        decimal finalcost = cost * Convert.ToDecimal(textBox3.Text);
        decimal finalretail = retail * Convert.ToDecimal(textBox3.Text);
        decimal finaldiscount = discount;
        decimal finalamount = finalretail + finaldiscount;
        MySqlConnection connection1 = new MySqlConnection(myconnectionstring);

        string inputprod = "SELECT @sno SNo,ir.product_name Product_Name,iv.unit_qty Unit_Qty, iv.uom UOM,@total_qty Total_Qty,@retail Cost,@discount Discount,@amount Amount,ir.plu_code Plu_Code,iv.barcode Barcode,iv.description Description,@cost OrgCost,ir.dept_id Dept_ID,ir.cat_id Cat_ID,ir.primary_vendor_id Supplier_ID FROM inventory_register ir,inventory_value iv WHERE iv.barcode = ir.barcode AND ir.barcode = @barcode";
        connection1.Open();
        MySqlCommand cmd = new MySqlCommand(inputprod, connection1);
        cmd.Parameters.AddWithValue("@total_qty",decimal.Parse(textBox3.Text));
        cmd.Parameters.AddWithValue("@barcode", textBox2.Text);
        cmd.Parameters.AddWithValue("@sno", textBox1.Text);
        cmd.Parameters.AddWithValue("@cost", finalcost);
        cmd.Parameters.AddWithValue("@retail",finalretail);
        cmd.Parameters.AddWithValue("@discount",finaldiscount);
        cmd.Parameters.AddWithValue("@amount",finalamount);
        MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
        MySqlCommandBuilder cbuilder = new MySqlCommandBuilder(adapter);

        adapter.Fill(dt);
        dataGridView1.DataSource = dt;
        connection1.Close();
        DataGridViewColumn colS_NO = dataGridView1.Columns[0];
        colS_NO.Width = 50;
        DataGridViewColumn colProduct = dataGridView1.Columns[1];
        colProduct.Width = 200;

        dataGridView1.Columns[7].Visible = false;
        dataGridView1.Columns[8].Visible = false;
        dataGridView1.Columns[9].Visible = false;
        dataGridView1.Columns[10].Visible = false;
        dataGridView1.Columns[11].Visible = false;
        dataGridView1.Columns[12].Visible = false;
        dataGridView1.Columns[13].Visible = false;
        dataGridView1.Columns[14].Visible = false;
        decimal TotNo = decimal.Parse("0.00");
        for (int i = 0; i < dataGridView1.RowCount - 1; i++)
        {
            TotNo = TotNo + Convert.ToDecimal(dataGridView1.Rows[i].Cells[7].Value);

        }
        lblNFqtyNo.Text = NFQTYNo.ToString();
        lblStotNo.Text = SubTot.ToString();
        lblDiscNo.Text = DiscNo.ToString();
        lblTotNo.Text = TotNo.ToString();

       }

enter image description here

1 个答案:

答案 0 :(得分:0)

请检查此行。 dataGridView1.DataSource = dt;来自DB的是什么,你将其设置为数据源。在这里,我认为不是再次设置数据源,而是需要在datagridview中添加一行。像dataGridView1.Rows.Add(//使用你想要的新值创建新行)

或者您可以使用以下代码段。

var index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["Sno"].Value = 2;
dataGridView1.Rows[index].Cells["Product_Name"].Value = "DHAWAT Brown Rice";