Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to enter the Product details from textbox to gridview row.
If the same Product already in gridview, just update the quantity and rate.
else enter the new row.
 

but this code not working properly. I need help.
Thank you in advance...

What I have tried:

<pre lang="c#">foreach (DataGridViewRow row1 in dgvmaingrid.Rows)  
{  
 if (row1.Cells["PNAME"].Value.Equals(txtProdsearch.Text.Trim()) && row1.Cells["DESC"].Value.Equals(txtDesc.Text.Trim()))  
{  
row1.Cells["QTY1"].Value = (double.Parse(row1.Cells["QTY1"].Value.ToString()) + double.Parse(txtQty.Text));  
row1.Cells["NETAMT"].Value = Convert.ToDecimal(String.Format("{0:0.00}", Convert.ToDecimal((double.Parse(row1.Cells["NETAMT"].Value.ToString()) + double.Parse(txtNetPrice.Text))))).ToString();  
return;  
}  
  
else if (row1.Cells["PNAME"].Value.ToString() != (txtProdsearch.Text.Trim()) && row1.Cells["DESC"].Value.ToString() != (txtDesc.Text.Trim()))  
 {  
                    dgvmaingrid.Rows.Add();  
                    
                    snoadd = snoadd + 1;  
                    row = dgvmaingrid.RowCount - 1;  
                    dgvmaingrid.Rows[row].Cells["sno"].Value = snoadd;  
                    dgvmaingrid.Rows[row].Cells["PNAME"].Value = txtProdsearch.Text;  
                    dgvmaingrid.Rows[row].Cells["DESC"].Value = txtDesc.Text;  
                    dgvmaingrid.Rows[row].Cells["QTY1"].Value = txtQty.Text;  
                    dgvmaingrid.Rows[row].Cells["RATE1"].Value = txtUPrice.Text;  
                    double neti = double.Parse(txtNetPrice.Text);  
                    dgvmaingrid.Rows[row].Cells["NETAMT"].Value = Convert.ToDecimal(String.Format("{0:0.00}", Convert.ToDecimal(neti))).ToString();  
                    return;  
                }  
            }
Posted
Updated 12-Jul-18 12:43pm

1 solution

Two immediate problems:
i) You check row by row and if that particular row doesn't match, you add a new row.
ii) The adding a new row happens inside the iteration of the row collection - very bad move!

Possible solution

C#
var found = false;
foreach (DataGridViewRow row1 in dgvmaingrid.Rows)  
{  
 if (row1.Cells["PNAME"].Value.Equals(txtProdsearch.Text.Trim()) && 
    row1.Cells["DESC"].Value.Equals(txtDesc.Text.Trim()))  
  {  
  row1.Cells["QTY1"].Value = (double.Parse(row1.Cells["QTY1"].Value.ToString()) + 
     double.Parse(txtQty.Text));  
  row1.Cells["NETAMT"].Value = Convert.ToDecimal(String.Format("{0:0.00}", 
     Convert.ToDecimal((double.Parse(row1.Cells["NETAMT"].Value.ToString()) + 
     double.Parse(txtNetPrice.Text))))).ToString();  
  found = true;
  break;
  }
}
if (!found) 
{
//add new row ....
}
 
Share this answer
 
Comments
Kingever512 27-Jul-18 10:04am    
Thank you Andrew Baylis.... Code works well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900