简体   繁体   中英

trouble Adding new row to datagridview in runtime using c#

I am trying to add rows to the datagridview on textbox leave event. everytime i leave the textbox the values correxponding to the text entered in textbox should get added to the grid one by one. but in my case when i leave the textbox on second entry, the first row added earlier in the grid disappears and the second row only is added. the following is the code. Can anyone guide me in what mistake i am doing here?

    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();

       }

在此输入图像描述

Please check this line . dataGridView1.DataSource = dt; what ever is coming from DB you are setting that as data source. Here I think instead of setting the Data source again you need to do is add a row to the datagridview . like dataGridView1.Rows.Add( //make the new row with new values you want )

or you can use the following snippet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM