叠加dgv中相同的行信息

俗话说,磨刀不误砍柴工,先说一下情况。点击按钮后往dgv中添加一行(行中字段含有数量),再点击一次,又添加一行。

假如这两条信息一样。则要进行叠加(数量相加)。我的思路是这样的:每次点击一次就往dgv添加一行,同时拿到行的主键ID。

当再添加一行时,就遍历dgv中的行,拿每行的主键ID和点击时的主键ID作比较,如果相同,就获取这行的数量,加到已有的行

中去,最后移除这行。否则,直接添加一行。

 1  private void btnzengJia_Click(object sender, EventArgs e)
 2         {
 3             decimal WholesalePrice = 0;//定义零售总价
 4             decimal RetailPrice = 0;//定义批发总价
 5             int newShockCount = 0;//定义新库存数
 6             if (txtYaoPinDaiMa .Text.ToString(). Trim () !=null &&txtBaoZhuangDanWei  .Text.ToString().Trim ()!=""
 7                 &&txtChuKuShu  .Text !="")
 8             {
 9                 if(txtChuKuShu.Text.Trim ().Length <9)
10                 {
11                     if (Convert.ToInt32(txtKuCunShu.Text.Trim().ToString()) >=
12                     Convert.ToInt32(txtChuKuShu.Text.Trim().ToString()))
13                    {//绑定数据
14                      dgvLingYao.Rows.Add();
15                         int dgvPlaceRowCountRow = dgvLingYao.Rows.Count - 1;
16                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["药品名称"].Value = txtYaoPinMingCheng.Text.Trim();
17                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["药品规格"].Value = txtGuiGe.Text.Trim();
18                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["批发价"].Value = txtPiFaJia.Text.Trim();
19                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["零售价"].Value = txtLingShouJia.Text.Trim().ToString ();
20                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["包装单位"].Value = txtBaoZhuangDanWei.Text.Trim();
21                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["出库数量"].Value = txtChuKuShu.Text.Trim();
22                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["备注"].Value = txtBeiZhu.Text.Trim();
23                         dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["药品ID"].Value = intDrugID;
24                         //计算新库存数
25                     newShockCount = Convert.ToInt32(txtKuCunShu.Text.Trim().ToString())
26                                   - Convert.ToInt32(txtChuKuShu.Text.Trim().ToString());
27                     txtKuCunShu.Text = newShockCount.ToString();
28                         int updatecount = myfrmGongYaoLinYongShengQingClient.updateShockCount(intDrugID, Convert.ToInt32
29                                       (txtKuCunShu.Text.Trim().ToString()));//修改库存数                      
30                         foreach (DataGridViewRow dgvr in dgvLingYao.Rows)
31                     {
32                         if (dgvr.Cells["批发价"].Value != null)//计算批发价总额
33                         {
34                             WholesalePrice += Convert.ToDecimal(dgvr.Cells["批发价"].Value) * Convert.ToInt32(dgvr.Cells["出库数量"].Value);
35                         }
36                         if (dgvr.Cells["零售价"].Value != null)//计算零售价总额
37                         {
38                             RetailPrice += Convert.ToDecimal(dgvr.Cells["零售价"].Value) * Convert.ToInt32(dgvr.Cells["出库数量"].Value);
39                         }
40                     }
41                    //绑定数据到文本                
42                     LbWholesalePrices.Text = WholesalePrice.ToString();
43 
44                     LbRetailPrice.Text = RetailPrice.ToString();
45                     LbMargin.Text = (WholesalePrice - RetailPrice).ToString();                      
46                             int intlock = 0;//定义一个整形锁(循环数)
47 
48                             foreach (DataGridViewRow dgvr in dgvLingYao.Rows)
49                             {
50                                 intlock++;
51                                 if (Convert.ToInt32(dgvr.Cells["药品ID"].Value) == intDrugID)
52                                 {
53                                     if (intlock < dgvLingYao.Rows.Count)//循环到倒数第二条
54                                     {
55                                     //相加库存数
56                                         dgvr.Cells["出库数量"].Value = Convert.ToInt32(dgvr.Cells["出库数量"].Value)
57                                         + Convert.ToInt32(dgvLingYao.Rows[dgvPlaceRowCountRow].Cells["出库数量"].Value);
58                                         dgvLingYao.Rows.RemoveAt(dgvPlaceRowCountRow);//移除相同的那行数据
59                                     }
60                                 }
61                             }
62                             LbRecordCount.Text = dgvLingYao.Rows.Count.ToString();
63                         }             
64                 else
65                 {
66                     panel2.Visible = true;
67                     label18.Text = "库存不足";
68                     ShowTime = DateTime.Now;
69                 }
70              }
71                 else
72                 {
73                     panel2.Visible = true;
74                     label18.Text = "出库数太大";
75                     ShowTime = DateTime.Now;
76                 }
77             }      
78             else
79             {            
80                 panel2.Visible = true;
81                 label18.Text = "填完整再增加";
82                 ShowTime = DateTime.Now;
83             }        
84         }
原文地址:https://www.cnblogs.com/flytop/p/8532836.html