A set of .NET Framework managed libraries for developing graphical user interfaces.
Hi @OmkarHcl , Welcome to Microsoft Q&A.
Because cellpainting will cover the above onpaint event.
Add judgment, do not overwrite the first column, modify the code to:
private void CustomControl1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex > -1)
{
e.PaintBackground(e.CellBounds, true);
Rectangle r = e.CellBounds;
r.X -= 2;
// Check if it's the first column
if (e.ColumnIndex == 0)
{
// Align the text to the right for the first column
TextRenderer.DrawText(e.Graphics, this.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, r, e.CellStyle.ForeColor, TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
}
else
{
// Align the text to the left for other columns
TextRenderer.DrawText(e.Graphics, this.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, r, e.CellStyle.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
e.Handled = true;
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
**Note: Please follow the steps in our documentation to enable e-mail notifications