Example #1
0
        private void DrawCell(DataGridViewCellPaintingEventArgs e)
        {
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
            {
                e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }

            if (!MergeColumnNames.Contains(Columns[e.ColumnIndex].Name))
            {
                return;
            }

            var rect = e.CellBounds;

            var g = e.Graphics;

            var upRowNum = GetUpRowNum(e.RowIndex, e.ColumnIndex);

            var downRowNum = GetDownRowNum(e.RowIndex, e.ColumnIndex);

            var tag = Columns[e.ColumnIndex].Tag.ToString();

            if (!string.IsNullOrEmpty(tag))
            {
                var index = Columns[tag]?.Index;

                upRowNum = GetUpRowNum(e.RowIndex, index ?? 0);

                downRowNum = GetDownRowNum(e.RowIndex, index ?? 0);
            }

            var backBrush = new SolidBrush(e.CellStyle.BackColor);

            // Fills by background color.
            g.FillRectangle(backBrush, rect);

            // Draws string.
            DrawString(e, upRowNum, downRowNum);

            // Declares a grid pen.
            var gridPen = new Pen(GridColor);

            // Draws bottom line.
            if (downRowNum == 1)
            {
                g.DrawLine(gridPen, rect.Left, rect.Bottom - 1, rect.Right, rect.Bottom - 1);
            }

            // Draws right line.
            if (e.ColumnIndex < ColumnCount - 1)
            {
                g.DrawLine(gridPen, rect.Right - 1, rect.Top, rect.Right - 1, rect.Bottom);
            }
        }
Example #2
0
        /// <summary>
        /// 画单元格
        /// </summary>
        /// <param name="e"></param>
        private void DrawCell(DataGridViewCellPaintingEventArgs e)
        {
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
            {
                e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
            Brush gridBrush = new SolidBrush(GridColor);
            var   backBrush = new SolidBrush(e.CellStyle.BackColor);
            var   fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int   cellwidth;
            //上面相同的行数
            var upRows = 0;
            //下面相同的行数
            var downRows = 0;
            //总行数
            var count = 0;

            if (MergeColumnNames.Contains(Columns[e.ColumnIndex].Name) && e.RowIndex != -1)
            {
                cellwidth = e.CellBounds.Width;
                var gridLinePen = new Pen(gridBrush);
                var curValue    = e.Value == null ? "" : e.Value.ToString().Trim();
                var curSelected = CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim();
                if (!string.IsNullOrEmpty(curValue))
                {
                    #region 获取下面的行数

                    for (var i = e.RowIndex; i < Rows.Count; i++)
                    {
                        if (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 < Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : Rows[i].Cells[e.ColumnIndex].Size.Width;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    #endregion
                    #region 获取上面的行数

                    for (var i = e.RowIndex; i >= 0; i--)
                    {
                        if (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 < Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : Rows[i].Cells[e.ColumnIndex].Size.Width;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    #endregion
                    count = downRows + upRows - 1;
                    if (count < 2)
                    {
                        return;
                    }
                }
                if (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;
            }
        }