我想知道当我将数据加载到datagridview以更新它们时,在向现有行添加新行之后,如何更新新添加的行而不重复现有行。这是我的代码
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);
}
}
additional- 在我的程序中有datagridview用于插入,更新或删除Purchase Order,它的行为就像购物车一样。这个datagridview包含BookName,ISBNNo,订单数量,单价和总数。当我将这个datagridview数据插入数据库时,每行插入具有标识值的唯一ID PO_Cart_No(pk)。
还有其他背景文本框详细信息,即Po_No,supplier_Id和Grand total details插入到单独的表中,称为PO表。此外,这两个表与外键PO_No链接。我的问题是当我向现有行添加新行时更新数据库,没有插入新行,只更新了其他行数据。