Awybin
Awybin

Reputation: 87

How to add padding to the background color of a grid in winforms

I'm trying to use padding in a dataGridView in winforms so that when I change the backColor property of a cell it doesn't paint the whole cell. However the padding property seems to only work with the text of the cell and not the background color... Is there any way to make this work?

Upvotes: 0

Views: 344

Answers (1)

Awybin
Awybin

Reputation: 87

I menaged to do it using the CellPainting method of the grid

    private void Grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        // If it's not the column you want or it's the header
        if (e.ColumnIndex != _grid.Columns["Deadline"].Index || e.RowIndex < 0)
            return;

        Color foreColor = e.CellStyle.ForeColor;
        Color rectangleColor = ColorTranslator.FromHtml("#80AC7B");
        Color backColor;
        if (_grid.Rows[e.RowIndex].Selected)
            backColor = ColorTranslator.FromHtml("#DFF5E0");
        else
            backColor = ColorTranslator.FromHtml("#FFFFFF");

        // Rectangle to keep the background the same as the rest of the grid
        Rectangle backgroundRect = new Rectangle(e.CellBounds.X,
            e.CellBounds.Y, e.CellBounds.Width,
            e.CellBounds.Height);

        // Rectangle that will be colored above the first one
        // Padding of 5 for all borders
        Rectangle coloredRect = new Rectangle(e.CellBounds.X + 5,
            e.CellBounds.Y + 5, e.CellBounds.Width - 10,
            e.CellBounds.Height - 10);

        using (
            SolidBrush textBrush = new SolidBrush(foreColor),
            backColorBrush = new SolidBrush(backColor),
            coloredRectBrush = new SolidBrush(rectangleColor),
            gridBrush = new SolidBrush(_grid.GridColor))
        {
            // Draw the background rectangle
            e.Graphics.FillRectangle(backColorBrush, backgroundRect);

            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // Draw the grid lines (only the right and bottom lines;
                // DataGridView takes care of the others).
                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);
            }

            // Draw the colored rectangle
            e.Graphics.FillRectangle(coloredRectBrush, coloredRect);

            // Set center alignment for the text
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;

            // Draw the text in the cell above the colored rectangle
            if (e.Value != null)
                e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, textBrush, coloredRect, sf);
            else
                e.Graphics.DrawString("-", e.CellStyle.Font, textBrush, coloredRect, sf);

            e.Handled = true;
        }
    }

Upvotes: 2

Related Questions