• datagridview 纵向 横向 合并单元格


    datagridview 单元格合并:纵向以及横向合并参考了csdn上不知哪位的代码,具体哪位找不到连接了。

    纵向合并:

            /// <summary>
            /// 纵向合并,即合并数据项的值
            /// </summary>
            /// <param name="e"></param>
            private void DrawCellVer(DataGridViewCellPaintingEventArgs e)
            {
                if (e.CellStyle.Alignment != DataGridViewContentAlignment.MiddleCenter)
                {
                    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                Brush gridBrush = new SolidBrush(this.GridColor);
                SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
                int cellwidth;
                //上面相同的行数
                int UpRows = 0;
                //下面相同的行数
                int DownRows = 0;
                //总行数
                int count = 0;
                if (true)
                {
                    cellwidth = e.CellBounds.Width;
                    Pen gridLinePen = new Pen(gridBrush);
                    //获取当前单元格的值,如果为null,就赋值为""
                    string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                    if (!string.IsNullOrEmpty(curValue))
                    {
                        #region 获取下面的行数
                        for (int i = e.RowIndex; i < this.Rows.Count; i++)
                        {
                            if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                            {
                                DownRows++;
                                if (e.RowIndex != i)
                                {
                                    cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
                                }
                            }
                            else
                                break;
                        }
                        #endregion
    
                        #region 获取上面的行数
                        for (int i = e.RowIndex; i >= 0; i--)
                        {
                            if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                            {
                                UpRows++;
                                if (e.RowIndex != i)
                                {
                                    cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
                                }
                            }
                            else
                                break;
                        }
                        #endregion
                        count = DownRows + UpRows - 1;
    
                        if (count < 2)
                        {
                            return;
                        }
                    }
                    else
                    {
                        //取下面看是否有为空的单元格,如果为最后一个为空的单元格,则画下边线
                        if (e.RowIndex < this.Rows.Count - 1)
                        {
                            if (this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != DBNull.Value)
                            {
                                if (!string.IsNullOrEmpty(this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString()))
                                {
                                    DownRows = 1;
                                }
    
                            }
                        }
                    }
                    if (this.Rows[e.RowIndex].Selected)
                    {
                        backBrush.Color = e.CellStyle.SelectionBackColor;
                        //fontBrush.Color = e.CellStyle.SelectionForeColor;
                    }
                    //以背景色填充
                    e.Graphics.FillRectangle(backBrush, e.CellBounds);
                    //画字符串
                    PaintingFont(e, cellwidth, UpRows, DownRows, count);
                    if (DownRows == 1)
                    {
                        //画下面的线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        count = 0;
                    }
                    // 画右边线
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
    
                    e.Handled = true;
                }
            }
    

      横向合并:

            /// <summary>
            /// 水平合并单元格,即数据头的合并
            /// </summary>
            /// <param name="e"></param>
            private void DrawCellHor(DataGridViewCellPaintingEventArgs e)
            {
                if (e.CellStyle.Alignment != DataGridViewContentAlignment.MiddleCenter)
                {
                    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                Brush gridBrush = new SolidBrush(this.GridColor);
                SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
                int cellwidth;
                //前面相同的行数
                int BefRows = 0;
                //后面相同的行数
                int BehRows = 0;
                //总列数
                int count = 0;
                if (true)
                {
                    cellwidth = e.CellBounds.Width;
                    Pen gridLinePen = new Pen(gridBrush);
                    //获取当前单元格的值,如果为null,就赋值为""
                    string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                    if (!string.IsNullOrEmpty(curValue))
                    {
                        #region 获取后面的列数
                        for (int i = e.ColumnIndex; i < this.ColumnCount; i++)
                        {
                            if (this.Rows[e.RowIndex].Cells[i].Value.ToString().Equals(curValue))
                            {
                                BehRows++;
                                if (e.ColumnIndex != i)
                                {
                                    cellwidth = cellwidth < this.Rows[e.RowIndex].Cells[i].Size.Width ? cellwidth : this.Rows[e.RowIndex].Cells[i].Size.Width;
                                }
                            }
                            else
                                break;
                        }
                        #endregion
    
                        #region 获取前面的列数
                        for (int i = e.ColumnIndex; i >= 0; i--)
                        {
                            if (this.Rows[e.RowIndex].Cells[i].Value.ToString().Equals(curValue))
                            {
                                BefRows++;
                                if (e.ColumnIndex != i)
                                {
                                    cellwidth = cellwidth < this.Rows[e.RowIndex].Cells[i].Size.Width ? cellwidth : this.Rows[e.RowIndex].Cells[i].Size.Width;
                                }
                            }
                            else
                                break;
                        }
                        #endregion
                        count = BehRows + BefRows - 1;
    
                        if (count < 2)
                        {
                            return;
                        }
                    }
                    else
                    {
                        //取右边看是否有为空的单元格,如果为最后一个为空的单元格,则画右侧线
                        if (e.ColumnIndex < this.ColumnCount - 1)
                        {
                            if (this.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value != DBNull.Value)
                            {
                                if (!string.IsNullOrEmpty(this.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString()))
                                {
                                    BehRows = 1;
                                }
    
                            }
                        }
                    }
                    if (this.Rows[e.RowIndex].Selected)
                    {
                        backBrush.Color = e.CellStyle.SelectionBackColor;
                        //fontBrush.Color = e.CellStyle.SelectionForeColor;
                    }
                    //以背景色填充
                    e.Graphics.FillRectangle(backBrush, e.CellBounds);
                    //画字符串
                    HorPaintingFont(e, cellwidth, BefRows, BehRows, count);
                    if (BehRows == 1)
                    {
                        //画右边缘的线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                        count = 0;
                    }
                    // 画下边缘的线
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
    
                    e.Handled = true;
                }
            }
    

     单元格写值纵向:

            /// <summary>
            /// 绘制合并以后的值(纵向)
            /// </summary>
            /// <param name="e"></param>
            /// <param name="cellwidth"></param>
            /// <param name="UpRows"></param>
            /// <param name="DownRows"></param>
            /// <param name="count"></param>
            private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
            {
                if (e.Value != DBNull.Value)
                {
                    stValue = e.Value.ToString();
                }
                using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
                {
                    fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
                    fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
                    int cellheight = e.CellBounds.Height;
    
                    if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
                    {
    
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
                    }
                    else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
                    }
                    else
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                    }
                }
            }
    

      单元格写值横向:

            /// <summary>
            /// 水平方向写值
            /// </summary>
            /// <param name="e"></param>
            /// <param name="cellwidth"></param>
            /// <param name="UpRows"></param>
            /// <param name="DownRows"></param>
            /// <param name="count"></param>
            private void HorPaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
            {
                using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
                {
                    fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
                    fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
                    int cellHeight = e.CellBounds.Height;
    
                    if (e.Value != DBNull.Value)
                    {
                        stValue = e.Value.ToString();
                    }
                    if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
                    {
                        e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X - cellwidth * (UpRows - 1) + (cellwidth * count - fontwidth) / 2, e.CellBounds.Y + (cellHeight - fontheight) / 2);
                    }
                }
            }
    

      在次备忘一下。

  • 相关阅读:
    java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract
    Spring MVC controller控制器映射无法访问问题!!!
    关于 use-default-filters 属性的说明
    Spring MVC工程 无法拦截到url请求
    Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
    Spring事务
    MySQL几个join
    解决CentOS7关闭/开启防火墙出现Unit iptables.service failed to load: No such file or directory.
    【Mybatis架构】Mapper映射文件中的#{}与${}
    空指针异常(从辅助类获取对象之后需要实例化才能保存信息)
  • 原文地址:https://www.cnblogs.com/xiaojt/p/4466688.html
Copyright © 2020-2023  润新知