Hi,@OmkarHcl. You can subscribe to the CellPainting event of the DataGridView control and specify the dataGridView1_CellPainting method as the event handler.
I set the color of the pen that draws the header to red in order to see the effect more clearly.
using (Pen topBorderPen = new Pen(Color.Red, 2f)) using (Pen bottomBorderPen = new Pen(Color.Red, 1f))
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= 0)
{
// e.PaintBackground(e.CellBounds, true);
using (Brush headerBrush = new SolidBrush(Color.LightSalmon))
{
e.Graphics.FillRectangle(headerBrush, e.CellBounds);
}
using (Pen topBorderPen = new Pen(Color.Red, 2f))
using (Pen bottomBorderPen = new Pen(Color.Red, 1f))
{
e.Graphics.DrawLine(topBorderPen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Top);
e.Graphics.DrawLine(bottomBorderPen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
TextRenderer.DrawText(e.Graphics, e.Value.ToString(), e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
e.Handled = true;
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.