zoukankan      html  css  js  c++  java
  • C# DataGridView 合并相同的行

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Value.ToString() != string.Empty)
    {
    #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;//总行数
    using (Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
    {
    using (Pen gridLinePen = new Pen(gridBrush))
    {
    //清除单元格
    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);
    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.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
    e.Handled = true;
    }
    }
    #endregion
    }
    }

  • 相关阅读:
    《精通CSS:高级Web标准解决方案》系列(02):可视化格式模型
    《C#入门经典(第5版)》系列(10):定义类成员
    《C#入门经典(第5版)》系列(11):集合、比较和转换
    《锋利的jQuery》系列(01):jQuery选择器
    《C#入门经典(第5版)》系列(09):定义类
    C# 通过搜狐微博api远程更换壁纸
    C# 联网五子棋
    C# 新浪微博群发器
    C# seo测试小工具1:同时更新多网站的博客(csdn,cnblogs,163,sina)
    MS Ajax 客户端编程 学习笔记 (3)
  • 原文地址:https://www.cnblogs.com/Elcser/p/13347116.html
Copyright © 2011-2022 走看看