Andrew Truckle
Andrew Truckle

Reputation: 19197

Getting the GridColor property from a DataGridView from within the derived cell object

Now that I have converted my code into a custom DataGridView Column and Cell, I have a property that is not available in the Paint event:

class DataGridViewColourComboBoxCell : DataGridViewComboBoxCell
{
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        //base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        Rectangle rDraw;

        rDraw = Rectangle.FromLTRB(cellBounds.Left, cellBounds.Top, cellBounds.Right, cellBounds.Bottom - 1);

        //Pen penGridlines = new Pen(dataGridView.GridColor);
        graphics.DrawRectangle(Pens.Black, rDraw);
        //penGridlines.Dispose();
    }
}

When I was using the CellPainting event in the DVG I was able to use:

    Pen penGridlines = new Pen(dataGridView.GridColor);
    g.DrawRectangle(Pens.Black, rDraw);
    penGridlines.Dispose();

But I can't work out how to gain access to the DataGridView object so that I can get the GridColor property value.

Update:

I found on here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.gridcolor%28v=vs.110%29.aspx that the default colour for grid lines is SystemColors.ControlDarkDark. So I now have:

Pen penGridlines = new Pen(SystemColors.ControlDarkDark); // dataGridView.GridColor
graphics.DrawRectangle(penGridlines, rDraw);
graphics.FillRectangle(brBack, rDraw);
penGridlines.Dispose();

But could we use the GridColor property though in this context?

Upvotes: 0

Views: 58

Answers (1)

Asheesh Garg
Asheesh Garg

Reputation: 46

You can get the grid color like this inside paint method : this.DataGridView.GridColor

You can then use it like. Pen penGridlines = new Pen(this.DataGridView.GridColor);

Upvotes: 1

Related Questions