【问题标题】:adding multiple items one by one in datagridview in c# window form在c#窗口形式的datagridview中一一添加多个项目
【发布时间】: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


【解决方案1】:

我并不完全了解您要查找的内容,但如果您想在数据网格视图中添加多行,您需要创建一个数据行,然后您可以在数据网格中添加该行。 你可以参考这个Link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    相关资源
    最近更新 更多