0
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));
     }
}

I am trying to add multiple products one by one in a datagridview before saving it to database. But whenever I add a row in datagridview second time, the first row gets replaced by the second row -- it only works for single row addition.

TZHX
  • 5,291
  • 15
  • 47
  • 56
Vishki
  • 15
  • 6
  • 2
    You're not actually adding any rows... – TZHX Sep 30 '15 at 08:37
  • whenever i click save button,1 row appear in datagridview,m not adding it in database,i m retrieving values from textboxes,combos,n want to display it in datagridview. – Vishki Sep 30 '15 at 08:40
  • If you are trying to add a new product then you must add new DataGridViewRow everytime to datagridview. Here, you are looping through rows which is not required. – Rakesh Sep 30 '15 at 08:41
  • @Vishki What is `ProductAddGridView`? Where do you initialise it? Your `foreach` statement will go through the **existing rows** and change the values. – TZHX Sep 30 '15 at 08:43
  • ProductAddGridView is a gridview to show added products to list.it is like a cart for user,user should br able to add new products one by one in his list. – Vishki Sep 30 '15 at 08:46

1 Answers1

0

I am not exactly getting what are you looking for but if you want to add multiple row in to your datagrid view you will need to create a data row and then you can add that row in datagrid. You can refer this Link

Community
  • 1
  • 1