I'm not sure why you need to catch the CellPainting event to change the background color of the cell, just do it like that.
Daywisegrid.Rows[RowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
But if you want to do it in painting, try this
private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == 0 )
{
using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor))
{
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1);
e.PaintContent( e.ClipBounds );
e.Handled = true;
}
}
}
}
source
share