合并datagridview 条件合并行数据

        public void HeBing()
        {
            int rowsCount;
            int CellCount;
            rowsCount = FG1.Rows.Count;
            CellCount = FG1.Columns.Count;
            for (int a = 0; a < rowsCount; a++)
            { 
                string F_OnlyCode = FG1.Rows[a].Cells[1].Value.ToString();
                string F_Code = FG1.Rows[a].Cells[2].Value.ToString();
                double quanty = double.Parse(FG1.Rows[a].Cells[5].Value.ToString());
                if (quanty < 0)
                { 
                    for (int b = 0; b < rowsCount; b++)
                    {  
                        string F_Code1 = FG1.Rows[b].Cells[2].Value.ToString();
                        double quanty1 = double.Parse(FG1.Rows[b].Cells[5].Value.ToString());
                        string f_Onlycode = FG1.Rows[b].Cells[1].Value.ToString();
                        if (F_Code == F_Code1 && f_Onlycode != F_OnlyCode )
                        { 
                            //  出库计费等于退费
                            if (quanty1 == -quanty)
                            {
                                //FG1.Rows.RemoveAt(a);
                                FG1.Rows[a].Visible = false;

                                for (int c = 0; c < FG1.Rows.Count; c++)
                                {
                                    if (FG1.Rows[c].Cells[2].Value.ToString() == F_Code)
                                    {
                                        FG1.Rows[c].Visible = false;
                                       
                                    }
                                }
                            }
    
                            //出库计费大于退费
                            if (quanty1 > -quanty && quanty1 != quanty)
                            {
                                FG1.Rows[b].Cells[5].Value = (quanty - (-quanty1)).ToString("F3");
                                this.FG1.Rows[b].DefaultCellStyle.BackColor = Color.Aqua;
                                FG1.Rows[a].Visible = false;

                            }//出库计费小于退费
                            if (quanty1 < -quanty && quanty1 != quanty)
                            {

                                this.FG1.Rows[b].Cells[5].Value = ((quanty1) + (quanty)).ToString("F3");
                                //this.FG1.Rows.RemoveAt(a);
                                FG1.Rows[a].Visible = false;
                            }
                            if (quanty1 == quanty && quanty < 0 && quanty != quanty)
                            {
                                FG1.Rows[a].DefaultCellStyle.BackColor = Color.Aqua;
                            }
                          
                        }
                       
                    }
                }

            } 
            //只存在退费信息
            for (int u = 0; u < rowsCount; u++)
            {
                if (double.Parse(FG1.Rows[u].Cells[5].Value.ToString()) < 0)
                { 
                this.FG1.Rows[u].DefaultCellStyle.BackColor = Color.Red;
                }
            }
           

        }

四种合并情况:

出库计费等于退库计费

出库计费大于退库计费

出库计费小于退库计费

只存在退库计费

原文地址:https://www.cnblogs.com/hanke123/p/5702990.html