How to update datagridview if I add new row to existing rows in C#?

212 Views Asked by At

In my program which has datagridview for insert,update or delete Purchase Order and it behave like as a shopping cart.This datagridview consist of BookName,ISBNNo,Order quantity,unit price and total.When I insert this datagridview data to database,each row insert with unique id PO_Cart_No(pk) which has identity value.

And also other background textbox details which are Po_No,supplier_Id and Grand total details insert into separate table which called PO table.And also this two tables link with foreign key PO_No.My question is when I add new row to existing row to update database,that new row didn't insert and other row data has updated. Where is the problem.This is my code for you,

public void UpdatePurchseOrderTable(int PO_No,int Supplier_ID, string Date, string RequiredDate, double GrandTotal)
    {
        DynamicConnection con = new DynamicConnection();
        con.mysqlconnection();
        string query = "UPDATE TBL_PO "
            + " SET Supplier_ID = @Supplier_ID,Date = @Date,"
            + "RequiredDate = @RequiredDate,GrandTotal=@GrandTotal"
             + " WHERE PO_No = @PO_No";
        con.sqlquery(query);
        con.cmd.Parameters.Add(new SqlParameter("@PO_No", SqlDbType.Int));
        con.cmd.Parameters["@PO_No"].Value = PO_No;
        con.cmd.Parameters.Add(new SqlParameter("@Supplier_ID", SqlDbType.Int));
        con.cmd.Parameters["@Supplier_ID"].Value = Supplier_ID;
        con.cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.Date));
        con.cmd.Parameters["@Date"].Value = Date;
        con.cmd.Parameters.Add(new SqlParameter("@RequiredDate", SqlDbType.Date));
        con.cmd.Parameters["@RequiredDate"].Value = RequiredDate;
        con.cmd.Parameters.Add(new SqlParameter("@GrandTotal", SqlDbType.Money));
        con.cmd.Parameters["@GrandTotal"].Value = GrandTotal;
        con.nonquery();
    }

    public void UpdatePOCartTable(int PO_No,string ISBN_No,int OrderQuantity, double UnitPrice, double Total)
    {
        DynamicConnection con = new DynamicConnection();
        con.mysqlconnection();
        string query = "UPDATE TBL_PO_Cart"
            + " SET ISBN_No = @ISBN_No, OrderQuantity= @OrderQuantity,"
            + "UnitPrice= @UnitPrice,Total=@Total"
            + " WHERE PO_No = @PO_No";
        con.sqlquery(query);
        con.cmd.Parameters.Add(new SqlParameter("@PO_No", SqlDbType.Int));
        con.cmd.Parameters["@PO_No"].Value = PO_No;
        con.cmd.Parameters.Add(new SqlParameter("@ISBN_No", SqlDbType.NVarChar));
        con.cmd.Parameters["@ISBN_No"].Value = ISBN_No;
        con.cmd.Parameters.Add(new SqlParameter("@OrderQuantity", SqlDbType.NVarChar));
        con.cmd.Parameters["@OrderQuantity"].Value = OrderQuantity;
        con.cmd.Parameters.Add(new SqlParameter("@UnitPrice", SqlDbType.Money));
        con.cmd.Parameters["@UnitPrice"].Value = Math.Round(UnitPrice,2,MidpointRounding.AwayFromZero);
        con.cmd.Parameters.Add(new SqlParameter("@Total", SqlDbType.Money));
        con.cmd.Parameters["@Total"].Value = Math.Round(Total,2,MidpointRounding.AwayFromZero);
        con.nonquery();
    }

and PO form data as follows

 private void btnedit_Click(object sender, EventArgs e)
    {
        DynamicConnection con = new DynamicConnection();
        try
        {
            if (txtPONo.Text != "" || cmbsupID.Text != "" || date1.Text != "" || requireddate.Text != "" || txtgrandTotal.Text != "")
            {
                PurchaseOrder PO = new PurchaseOrder();
                if (cmbsupID.Text.Contains('-'))
                {
                    string str = cmbsupID.Text;
                    int index = str.IndexOf('-');
                    if (index > 0)
                    {
                        int value = int.Parse(str.Substring(0, index));
                        PO.UpdatePurchseOrderTable(Convert.ToInt32(txtPONo.Text), value, date1.Text, requireddate.Text, Convert.ToDouble(txtgrandTotal.Text));
                    }
                }
                else
                {
                    int value2 = Convert.ToInt32(cmbsupID.Text);
                    PO.UpdatePurchseOrderTable(Convert.ToInt32(txtPONo.Text), value2, date1.Text, requireddate.Text, Convert.ToDouble(txtgrandTotal.Text));
                }

                for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                {
                    int PONO = Convert.ToInt32(txtPONo.Text);
                    string column1 = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                    int column2 = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
                    double column3= Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
                    double column4 = Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
                    PO.UpdatePOCartTable(PONO,column1,column2,column3,column4);

                }
            }
            else
            {
                MessageBox.Show("Please Provide Details!");
            }
            dataGridView1.Rows.Clear();
            ClearData();
            retviewPO_No();
            MessageBox.Show("Record Updated Successfully");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
0

There are 0 best solutions below