在c#窗口形式的datagridview中逐个添加多个项目

本文关键字:添加 项目 datagridview 窗口 | 更新日期: 2023-09-27 18:15:28

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中添加一行时,第一行被第二行取代-它只适用于单行添加。

在c#窗口形式的datagridview中逐个添加多个项目

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