DataGridView为指定的区域合并单元格并设置文字居中

传参结构体

        #region <<单元格合并配置>>
        public struct MergeCellsParam
        {
            public int iStartCellColumn;
            public int iStartCellRow;
            public int iEndCellColumn;
            public int iEndCellRow;
            public string strText;

            public MergeCellsParam(int _iStartColumn, int _iStartRow, int _iEndColumn, int _iEndRow)
            {
                iStartCellColumn = _iStartColumn;
                iStartCellRow = _iStartRow;
                iEndCellColumn = _iEndColumn;
                iEndCellRow = _iEndRow;
                strText = null;
            }
        }
        public List<MergeCellsParam> mMergeCells = new List<MergeCellsParam>();
        #endregion

配置结构体成员参数

        #region 获取单元格合并所需的行列
        public void UpdateMergeCells()
        {
            mMergeCells.Clear();
            mMergeCells.Add(new MergeCellsParam(0, 0, 0, 7));//第0列第0行~第0列第7行
        }
        #endregion

单元格合并

        #region <<单元格合并>>
        private void dgvDO_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            for (int i = 0; i < mMergeCells.Count; i++)
            {
                if (mMergeCells[i].iStartCellRow == mMergeCells[i].iEndCellRow
                    && mMergeCells[i].iStartCellColumn == mMergeCells[i].iEndCellColumn)
                {
                    return;
                }
                var _mergeParam = mMergeCells[i];
                if (e.ColumnIndex >= _mergeParam.iStartCellColumn && e.ColumnIndex <= _mergeParam.iEndCellColumn
                    && e.RowIndex >= _mergeParam.iStartCellRow && e.RowIndex <= _mergeParam.iEndCellRow)
                {
                    FindRange(e, this.dgvDO);
                }
            }
        }
        #endregion

找到上下行内容相同的行

        #region 找到上下行内容相同的行
        private void FindRange(DataGridViewCellPaintingEventArgs e, DataGridView myGrid)
        {
            e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            Brush gridBrush = new SolidBrush(dgvDO.GridColor);
            SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int UpRows = 0;
            int DownRows = 0;
            int count = 0;
            int cellwidth = e.CellBounds.Width;
            Pen gridLinePen = new Pen(gridBrush);
            if (e.Value != null)
            {
                string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                //MessageBox.Show("curValue:" + curValue);
                #region 获取下面的行数
                for (int i = e.RowIndex; i < myGrid.Rows.Count; i++)
                {
                    if (myGrid.Rows[i].Cells[e.ColumnIndex].Value != null)
                    {
                        if (myGrid.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                        {
                            //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;

                            DownRows++;
                            if (e.RowIndex != i)
                            {
                                cellwidth = cellwidth < myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                #endregion
                #region 获取上面的行数
                for (int i = e.RowIndex; i > 0; i--)
                {
                    if (myGrid.Rows[i].Cells[e.ColumnIndex].Value != null)
                    {
                        if (myGrid.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                        {
                            //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;

                            UpRows++;
                            if (e.RowIndex != i)
                            {
                                cellwidth = cellwidth < myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                count = DownRows + UpRows - 1;
                if (count < 1)
                {
                    return;
                }
            }

            //if (GridView.Rows[e.RowIndex].Selected)
            // {
            //backBrush.Color = e.CellStyle.SelectionBackColor;
            backBrush.Color = Color.White;
            fontBrush.Color = e.CellStyle.SelectionForeColor;
            // }

            //以背景色填充
            e.Graphics.FillRectangle(backBrush, e.CellBounds);

            //画字符串
            if (e.RowIndex < 2)
            {
                PaintingFont(e, cellwidth, UpRows, DownRows, count - 1);
            }
            else
                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;
            #endregion
        }
        #endregion

写入字符串

        #region 画字符串
        // cellwidth = e.CellBounds.Width;
        // UpRows上面相同的行数
        // DownRows下面相同的行数
        // count 总行数
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
        {
            
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
            int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
            int cellheight = e.CellBounds.Height;
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
            {
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
            }
        }
        #endregion

实验

写一个8行4列的表dgvDO,看看能不能合并相同的项。

        private void Form1_Load(object sender, EventArgs e)
        {
            //InitDgvDO();
            dgvDO.Rows.Add("中国","","","");
            dgvDO.Rows.Add("中国", "", "", "");
            dgvDO.Rows.Add("美国", "", "", "");
            dgvDO.Rows.Add("美国", "", "", "");
            dgvDO.Rows.Add("英国", "", "", "");
            dgvDO.Rows.Add("英国", "", "", "");
            dgvDO.Rows.Add("英国", "", "", "");
            dgvDO.Rows.Add("英国", "", "", "");
            dgvDO.Columns[0].ReadOnly = true;//不设置成只读的话,点击单元格会看到隐藏的相同的字段
            dgvDO.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;//
            UpdateMergeCells();
        }

效果

成功

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值