Unable to hide the border of header

rahul kumar 605 Reputation points
2023-07-09T14:37:06.87+00:00

Hi , What should i had to add or modify my existing code in order to completely hide the borders of columnheaders . i have tried setting it to none but still a border is present between headers

User's image

  public partial class betterdatagridview : DataGridView
    {
        public betterdatagridview()
        {
            InitializeComponent();
            this.AllowUserToAddRows = false;
            this.AllowUserToResizeColumns = false;
            this.BorderStyle = BorderStyle.None;
            this.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.CellBorderStyle = DataGridViewCellBorderStyle.None;
        }
Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-10T09:36:44.6233333+00:00

    Hi @rahul kumar , Welcome to Microsoft Q&A.

    It's not a bezel, it's the gap left after the bezel was removed.

    You can use a brush to fill in.

    protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex == -1 && e.ColumnIndex > -1)
        {
            e.Handled = true;
            using (Brush b = new SolidBrush(this.DefaultCellStyle.BackColor))
            {
                e.Graphics.FillRectangle(b, e.CellBounds);
            }
            e.PaintContent(e.ClipBounds);
        }
    }
    

    User's image

    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 if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Batts, Jason 0 Reputation points
    2023-07-09T15:27:21.0866667+00:00

    /Good morning, have you tried adding this to your code?

    public partial class betterdatagridview : DataGridView

    {

    public betterdatagridview()
    
    {
    
        InitializeComponent();
    
        this.AllowUserToAddRows = false;
    
        this.AllowUserToResizeColumns = false;
    
        this.BorderStyle = BorderStyle.None;
    
        this.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
    
        this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
    
        this.CellBorderStyle = DataGridViewCellBorderStyle.None;
    
    
        
    
        // Add the following line to hide borders of column header cells
    
        this.ColumnHeadersDefaultCellStyle.BorderSides = DataGridViewHeaderBorderStyle.None;
    
    }
    

    }

    Try setting the ColumnHeadersDefaultCellStyle.BorderSides property to DataGridViewHeaderBorderStyle.None, you should be able to completely hide the borders of the column header cells. Hope this helps!

    -JayB

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

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