DataGridView 合并数据相同的行

private void dgvData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //需要合并行的判断
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Value.ToString() != string.Empty && e.ColumnIndex < 2 && e.RowIndex < dgvData.Rows.Count - 1)
            {
                int Cindex = e.ColumnIndex;
                string value = e.Value.ToString();

                #region
                int UpRows = 0;//上面相同的行数
                int DownRows = 0;//下面相同的行数
                int count = 0;//总行数
                int cellwidth = e.CellBounds.Width;//列宽
                //获取下面的行数
                for (int i = e.RowIndex; i < this.dgvData.Rows.Count - 1; i++)
                {
                    if (this.dgvData.Rows[i].Cells[Cindex].Value.ToString().Equals(value))
                    {
                        DownRows++;
                    }
                    else
                    {
                        break;
                    }
                }
                //获取上面的行数
                for (int i = e.RowIndex; i >= 0; i--)
                {
                    if (this.dgvData.Rows[i].Cells[Cindex].Value.ToString().Equals(value))
                    {
                        UpRows++;
                    }
                    else
                    {
                        break;
                    }
                }

                count = UpRows + DownRows - 1;//总行数
             
                using (Brush gridBrush = new SolidBrush(this.dgvData.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        
                        //清除单元格
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                        //清除合计行的序号
                        string vle = e.RowIndex == dgvData.Rows.Count - 1 && e.ColumnIndex == 0 ? "" : value;
                        if (vle != null)
                        {
                            int cellheight = e.CellBounds.Height;
                            SizeF size = e.Graphics.MeasureString(vle, e.CellStyle.Font);
                          
                            e.Graphics.DrawString((vle).ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + (cellwidth - size.Width) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - size.Height) / 2, StringFormat.GenericDefault);
                           

                        }

                        //如果下一行数据不等于当前行数据,则画当前单元格底边线
                        if (e.RowIndex < this.dgvData.Rows.Count - 2 && this.dgvData.Rows[e.RowIndex + 1].Cells[Cindex].Value.ToString() != value)
                        {
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            xh++;
                        }
                        if (e.RowIndex == this.dgvData.Rows.Count - 2)
                        {
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            count = 0;
                            xh = 0;
                        }
                        //画grid右边线
                        //e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                        e.Handled = true;
                    }
                }
                #endregion

            }
        }

效果图:

DataGridView 合并数据相同的行

 

 合并数据行是序号不一致的问题解决

if (dt != null && dt.Rows.Count > 0)
            {
                string xq=dt.Rows[0]["小区名称"].ToString();
                dt.Columns.Add("xh", typeof(int)).SetOrdinal(0);
                int xh=1;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["小区名称"].ToString() != xq) {
                        xh++;
                        xq = dt.Rows[i]["小区名称"].ToString();
                    }
                    dt.Rows[i]["xh"] = xh;
                }
                dgvData.DataSource = dt;
                dgvData.Refresh();
            }

原文出处:https://www.cnblogs.com/wyynts/p/6514977.html

上一篇:c# datagridview控件的学习心得


下一篇:c#-如何在一列中的两个文本框之间创建SqlSqlFilter