我设计了两个表(Sale和SaleList),这里是他们之间的关系(一对多)销售表是主人,第二个是孩子,Sale字段是(Id - SaleNum - CustomerName - 描述),SaleList字段是(Id - SaleId - Num - ItemName - Qut - Unit - SalePrice - Total),Iam手动将行添加到gridview并将项目数据从itemcard表传递给它,我想输入数据对数据库的行,并创建一个按钮来执行此操作,假设您按下按钮将数据保存到数据库中时添加两行,它只传输两行中的一行这是我的问题
private void simpleButton1_Click(object sender, EventArgs e)
{
Sale sale = new Sale()
{
SaleNum = int.Parse(txtSaleId.Text),
CustomerName = txtCustomer.Text,
Description = txtDescription.Text
};
db.Sales.InsertOnSubmit(sale);
db.SubmitChanges();
for (int i = 0; i < gvSale.DataRowCount; i++)
{
SaleList sList = new SaleList()
{
SaleId = int.Parse(gvSale.GetRowCellValue(i,"SaleId").ToString()),
//ItemId = int.Parse(row.Cells[3].Value.ToString()),
Num = gvSale.GetRowCellValue(i, "Num").ToString(),
ItemName = gvSale.GetRowCellValue(i, "ItemName").ToString(),
Qut = double.Parse(gvSale.GetRowCellValue(i, "Qut").ToString()),
Unit = gvSale.GetRowCellValue(i, "Unit").ToString(),
SalePrice = double.Parse(gvSale.GetRowCellValue(i, "SalePrice").ToString()),
Total = 0,
};
db.SaleLists.InsertOnSubmit(sList);
db.SubmitChanges();
}
}
问题是只将第一行中的数据插入数据库。