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

trouble Adding new row to datagridview in runtime using c#

我正在尝试在文本框离开事件时向 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();

       }

请检查这一行。 dataGridView1.DataSource = dt;无论来自数据库的是什么,您都将其设置为数据源。在这里,我认为您不需要再次设置数据源,而是向 datagridview 添加一行。 like 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";