且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用文本框值插入或更新gridview行值

更新时间:2023-12-06 15:23:22

两个直接问题:

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