【发布时间】:2015-10-01 17:04:43
【问题描述】:
private void btnSave1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in ProductAddGridView.Rows)
{
dgvr.Cells[0].Value = productIDTextBox.Text;
dgvr.Cells[1].Value = productTypeIDComboBox.Text;
dgvr.Cells[2].Value = companyIDComboBox.Text;
dgvr.Cells[3].Value = productPurchaseRateTextBox.Text;
dgvr.Cells[4].Value = productQtyTextBox.Text;
dgvr.Cells[5].Value = productDescriptionRichTextBox.Text;
dgvr.Cells[6].Value = Convert.ToString(Convert.ToUInt32(productQtyTextBox.Text.Trim()) * Convert.ToDecimal(productPurchaseRateTextBox.Text));
}
}
我正在尝试在数据网格视图中一一添加多个产品,然后再将其保存到数据库中。但是每当我第二次在 datagridview 中添加一行时,第一行就会被第二行替换——它只适用于单行添加。
【问题讨论】:
-
您实际上并没有添加任何行...
-
每当我单击保存按钮时,datagridview 中会出现 1 行,我没有将其添加到数据库中,我从文本框、组合中检索值,n 想在 datagridview 中显示它。
-
如果您尝试添加新产品,则必须每次向 datagridview 添加新的 DataGridViewRow。在这里,您正在遍历不需要的行。
-
@Vishki
ProductAddGridView是什么?你在哪里初始化它?您的foreach语句将遍历现有行并更改值。 -
ProductAddGridView 是一个网格视图,用于显示添加到列表中的产品。它就像用户的购物车,用户应该能够在他的列表中一个一个地添加新产品。
标签: c# datagridview rows