在C#中向datagridview添加新行时如何更新sql数据库?

时间:2018-06-02 13:09:47

标签: c# sql-server database datagridview

我想知道当我向从sql数据库表加载的现有行添加新行时如何更新数据库。从此代码开始,只更新现有行值并且不添加新行。

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 个答案:

没有答案