从派生的单元格对象中的DataGridView获取GridColor属性

时间:2016-06-09 17:14:04

标签: c# datagridview

现在我已将代码转换为自定义DataGridView 单元格,我有一个 Paint中不可用的属性事件:

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();
    }
}

当我在DVG中使用CellPainting事件时,我能够使用:

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

但我无法弄清楚如何获取对DataGridView对象的访问权限,以便获取GridColor属性值。

更新

我在这里找到:https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.gridcolor%28v=vs.110%29.aspx网格线的默认颜色是SystemColors.ControlDarkDark。所以我现在有:

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

但是我们可以在这种情况下使用GridColor属性吗?

1 个答案:

答案 0 :(得分:1)

你可以在paint方法中获得这样的网格颜色: this.DataGridView.GridColor

然后你可以像使用它一样。 Pen penGridlines = new Pen(this.DataGridView.GridColor);