zoukankan      html  css  js  c++  java
  • 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
    
                }
            }

    效果图:

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

    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

    后来发现了这一篇介绍的更加详细; 二维表头:https://www.cnblogs.com/dekevin/p/4028289.html

  • 相关阅读:
    JTAG的SWD接线方式
    Qt のEXecl
    人脸识别
    Qt实现基本QMainWindow主窗口程序
    Qt学习之路MainWindow学习过程中的知识点
    QT_FORWARD_DECLARE_CLASS
    标准的并发控制实现
    C++ DFS
    C# 互操作(一) 编写一个C++ COM组件
    Socket使用SOAP调用WCF
  • 原文地址:https://www.cnblogs.com/pyf97/p/14088496.html
Copyright © 2011-2022 走看看