【问题标题】:Getting the GridColor property from a DataGridView from within the derived cell object从派生的单元格对象中从 DataGridView 获取 GridColor 属性
【发布时间】:2016-06-09 17:14:03
【问题描述】:

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

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 属性吗?

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

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

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

    【讨论】:

    • 即使我在 Cell 对象内? DVG 在表单中。
    • 是的,“this.DataGridView”将为您提供此单元格所属的 DataGridview。这里的“this”仅指单元格对象。
    • 是的,如果它解决了您的问题陈述,请将其标记为答案。
    猜你喜欢
    • 2015-01-31
    • 2019-03-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多