Share via

borders fixing problem in datagridview

OmkarHcl 206 Reputation points
2023-06-02T14:02:23.5266667+00:00

I am trying to set the padding of my column's headers text in datagridview . What I had done is set the headertext of first column to right side successfully by using the below ..

   protected override void OnPaint(PaintEventArgs e)
        {
            // adjust the headers 
            this.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomRight;
             this.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            base.OnPaint(e);
        }

but the problem is i have also trying to reduce the padding margin of the remaining columns (to the left side ) and after introducing the below method , the above method is not working and all the headerText of all the columns have go to the left .

   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;
                TextRenderer.DrawText(e.Graphics, this.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, r, e.CellStyle.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                e.Handled = true;
            }
        }



I don't want the first column to move to the left side . Pls suggest what changes should i make in my code . for the reference i have also set the this.EnableHeadersVisualStyles = false;

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Anonymous
2023-06-05T08:28:34.87+00:00

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

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.