Winforms 如何更改DataGridView中某些单元格的边框颜色?

Winforms 如何更改DataGridView中某些单元格的边框颜色?,winforms,datagridview,Winforms,Datagridview,我需要在CellFormatting事件中更改某些单元格的边框颜色。单个单元格的电路板颜色可以更改吗?您可以绘制一个矩形。在本例中,我在选定的单元格上放置了一个红色的boder private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) {

我需要在CellFormatting事件中更改某些单元格的边框颜色。单个单元格的电路板颜色可以更改吗?

您可以绘制一个矩形。在本例中,我在选定的单元格上放置了一个红色的boder

private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected == true)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
            using (Pen p = new Pen(Color.Red, 1))
            {
                Rectangle rect = e.CellBounds;
                rect.Width -= 2;
                rect.Height -= 2;
                e.Graphics.DrawRectangle(p, rect);
            }
            e.Handled = true;
        }
    }
}

MSDN描述了一种方法,在该方法中,可以从DataGridView继承以覆盖默认边框样式:


不过,上面的绘制方法更简单。

此示例仅在选定单元格时有效。如果我需要创建多个矩形,该怎么办?@doro您只需更改条件:selected==true如何将相同的效果应用于RowHeightChanged和ColumnWidthChanged事件,例如?