使用文本框值插入或更新gridview行值 [英] Insert or update gridview row value with textbox value

查看:114
本文介绍了使用文本框值插入或更新gridview行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

两个直接问题:

i)你检查一行按行,如果该特定行不匹配,则添加一个新行。

ii)在行集合的迭代中添加新行 - 非常糟糕的移动!



可能的解决方案



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

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 ....
}


这篇关于使用文本框值插入或更新gridview行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆