Border not visible in datagridview

vijay kumar 120 Reputation points
2023-07-21T11:28:49.03+00:00

i have a custom datagridview with the following declaration

    
   private void uneditabledatagridview_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)
                { // Adjust the padding for the first column
                    r.X += 4;

                    // 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.Left | 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.Right | TextFormatFlags.VerticalCenter);
                }
                // Draw top double border
                int topBorderThickness = 3; // Set the thickness of the top border
                int bottomBorderThickness = 1; // Set the thickness of the bottom border
                int x = e.CellBounds.Left;
                int yTop = e.CellBounds.Top;
                int yBottom = e.CellBounds.Bottom - bottomBorderThickness;
                int width = e.CellBounds.Width;
                using (Pen topBorderPen = new Pen(Color.Black, topBorderThickness))
                using (Pen bottomBorderPen = new Pen(Color.Black, bottomBorderThickness))
                {
                    // Draw top border
                    e.Graphics.DrawLine(topBorderPen, x, yTop, x + width, yTop);

                    // Draw bottom border
                    e.Graphics.DrawLine(bottomBorderPen, x, yBottom, x + width, yBottom);
                }
                e.Handled = true;
            }
        }


it effects in this
User's image

the column header has a thick border on the upper side and slightly thin border on the bottom side . . but when the added this

        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);
            }
        }


to completely remove the header divisions between the cells , the header cells border goes

User's image

so i am trying to have the both effects on the header cells . can you pls suggest what to do

effect 1 : thick border on the upper and thin border on the bottom

effect 2 : completely remove the header cell division

the project is on

https://github.com/Vijaykumar4567/Vijaykumarproject

the related files are

  1. balancesheet.cs which is inside childforms folder
  2. uneditabledatagridview.cs which is inside supercontrols folder
Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2023-08-03T07:56:23.17+00:00

    Hi @vijay kumar , Welcome to Microsoft Q&A,

    You only need to delete the uneditabledatagridview_CellPainting event, and put the corresponding online and offline codes in the OncelPainting event.

    The original code doesn't work because you draw the rectangle again.

    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);
    		// Draw top double border
    		int topBorderThickness = 3; // Set the thickness of the top border
    		int bottomBorderThickness = 1; // Set the thickness of the bottom border
    		int x = e.CellBounds.Left;
    		int yTop = e.CellBounds.Top;
    		int yBottom = e.CellBounds.Bottom - bottomBorderThickness;
    		int width = e.CellBounds.Width;
    		using(Pen topBorderPen = new Pen(Color.Black, topBorderThickness))
    		using(Pen bottomBorderPen = new Pen(Color.Black, bottomBorderThickness))
    		{
    			// Draw top border
    			e.Graphics.DrawLine(topBorderPen, x, yTop, x + width, yTop);
    			// Draw bottom border
    			e.Graphics.DrawLine(bottomBorderPen, x, yBottom, x + width, yBottom);
    		}
    	}
    }
    

    enter image description here

    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 additional answers

Sort by: Most helpful

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.