现在我已将代码转换为自定义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
属性吗?
答案 0 :(得分:1)
你可以在paint方法中获得这样的网格颜色: this.DataGridView.GridColor
然后你可以像使用它一样。 Pen penGridlines = new Pen(this.DataGridView.GridColor);