• C#datagridview 合并数据相同的行


      private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Value.ToString() != string.Empty)
                {
                    //if (!string.IsNullOrEmpty(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    //{
                    #region
                    int UpRows = 0;//上面相同的行数
                    int DownRows = 0;//下面相同的行数
                    int count = 0;//总行数
                    int cellwidth = e.CellBounds.Width;//列宽
                    //获取下面的行数
                    for (int i = e.RowIndex; i < this.dataGridView1.Rows.Count; i++)
                    {
                        if (this.dataGridView1.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(e.Value.ToString()))
                        {
                            DownRows++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    //获取上面的行数
                    for (int i = e.RowIndex; i >= 0; i--)
                    {
                        if (this.dataGridView1.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(e.Value.ToString()))
                        {
                            UpRows++;
                        }
                        else
                        {
                            break;
                        }
                    }
    
                    count = UpRows + DownRows - 1;//总行数
                    //if (count < 2)
                    //{ return; }
                    using (Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value;
                            //this.dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value = DBNull.Value;
                            //清除单元格
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
    
    
                            if (e.Value != null)
                            {
                                int cellheight = e.CellBounds.Height;
                                SizeF size = e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font);
                                //if (e.RowIndex > 0 && this.dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())
                                //{
    
                                //}
                                //else
                                //{
                                e.Graphics.DrawString((e.Value).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.dataGridView1.Rows.Count - 1 && this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
                            {
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            }
                            if (e.RowIndex == this.dataGridView1.Rows.Count - 1)
                            {
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                count = 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行合并代码,已忘记是摘抄的网上的资料还是自己写的了,故此处无法写明参考自何处,如有哪位网友发现来源,麻烦请告知。在下还是很支持正版的。(整理进自己的博客里,方便查看阅读。O(∩_∩)O~~)

  • 相关阅读:
    阿里HBase高可用8年“抗战”回忆录
    Service Mesh 初体验
    阿里云HBase推出普惠性高可用服务,独家支持用户的自建、混合云环境集群
    Ververica Platform-阿里巴巴全新Flink企业版揭秘
    深度 | 带领国产数据库走向世界,POLARDB底层逻辑是什么?
    AI加持的阿里云飞天大数据平台技术揭秘
    Nacos 常见问题及解决方法
    数据上云,应该选择全量抽取还是增量抽取?
    一文带你了解 Flink Forward 柏林站全部重点内容
    Oracle数据库中序列(SEQUENCE)的用法详解
  • 原文地址:https://www.cnblogs.com/wyynts/p/6514977.html
Copyright © 2020-2023  润新知