Insert or Update Gridview value with textbox value

Jul 9 2018 5:37 AM
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.
 
the code I have used:
  1. foreach (DataGridViewRow row1 in dgvmaingrid.Rows)  
  2. {  
  3.  if (row1.Cells["PNAME"].Value.Equals(txtProdsearch.Text.Trim()) && row1.Cells["DESC"].Value.Equals(txtDesc.Text.Trim()))  
  4. {  
  5. row1.Cells["QTY1"].Value = (double.Parse(row1.Cells["QTY1"].Value.ToString()) + double.Parse(txtQty.Text));  
  6. 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();  
  7. return;  
  8. }  
  9.   
  10. else if (row1.Cells["PNAME"].Value.ToString() != (txtProdsearch.Text.Trim()) && row1.Cells["DESC"].Value.ToString() != (txtDesc.Text.Trim()))  
  11.  {  
  12.                     dgvmaingrid.Rows.Add();  
  13.                     
  14.                     snoadd = snoadd + 1;  
  15.                     row = dgvmaingrid.RowCount - 1;  
  16.                     dgvmaingrid.Rows[row].Cells["sno"].Value = snoadd;  
  17.                     dgvmaingrid.Rows[row].Cells["PNAME"].Value = txtProdsearch.Text;  
  18.                     dgvmaingrid.Rows[row].Cells["DESC"].Value = txtDesc.Text;  
  19.                     dgvmaingrid.Rows[row].Cells["QTY1"].Value = txtQty.Text;  
  20.                     dgvmaingrid.Rows[row].Cells["RATE1"].Value = txtUPrice.Text;  
  21.                     double neti = double.Parse(txtNetPrice.Text);  
  22.                     dgvmaingrid.Rows[row].Cells["NETAMT"].Value = Convert.ToDecimal(String.Format("{0:0.00}", Convert.ToDecimal(neti))).ToString();  
  23.                     return;  
  24.                 }  
  25.             }  
but this code not working properly. I need help.
Thank you in advance... 

Answers (9)