在 c# window 窗体的 datagridview 中一一添加多个项目

adding multiple items one by one in datagridview in c# window form

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 中添加一行时,第一行就会被第二行替换——它只适用于单行添加。

我不完全明白你在找什么,但如果你想在你的数据网格视图中添加多行,你将需要创建一个数据行,然后你可以在数据网格中添加该行。 你可以参考这个Link