Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the border color of some cells in DataGridView?

I need to programming change the border color of some cells in the CellFormatting event. Can the board color of an individual cell be changed?

like image 616
ca9163d9 Avatar asked Dec 07 '12 22:12

ca9163d9


1 Answers

You can draw a rectangle. In this example I put a red boder on the selected cells.

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;
        }
    }
}
like image 120
Alejandro del Río Avatar answered Nov 15 '22 05:11

Alejandro del Río