Henry Vuong

Henry Vuong

  • NA
  • 27
  • 0

Draw vertical bold line between some columns in datagridview

Jun 10 2019 8:36 PM
I want to draw vertical bold red lines between some columns in a datagridview. I created a form and a datagridview, and this is what I want to get:
 
 But this is what I get, notice the two vertical red lines in column header:
 
 
Here's my code:
  1. private void Form1_Load(object sender, EventArgs e)  
  2.  {  
  3.      //Add colums to dataGridView1  
  4.      this.dataGridView1.Columns.Add("Col_A""Column A");  
  5.      this.dataGridView1.Columns.Add("Col_B""Column B");  
  6.      this.dataGridView1.Columns.Add("Col_C""Column C");  
  7.      this.dataGridView1.Columns.Add("Col_D""Column D");  
  8.      this.dataGridView1.Columns.Add("Col_E""Column E");  
  9.      this.dataGridView1.Columns.Add("Col_F""Column F");  
  10.   
  11.      //Add rows to datagridview  
  12.      for (int i = 0; i < 50; i++)  
  13.      {  
  14.          this.dataGridView1.Rows.Add("A" + i.ToString(), "B" + i.ToString(), "C" + i.ToString(),   
  15.                                      "D" + i.ToString(), "E" + i.ToString(), "F" + i.ToString());  
  16.      }  
  17.   
  18.      //Set the width of columns  
  19.      for (int j = 0; j < this.dataGridView1.ColumnCount; j++)  
  20.      {  
  21.          this.dataGridView1.Columns[j].Width = 90;  
  22.      }  
  23.   
  24.      this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);  
  25.  }  
  26.   
  27.  //Cell painting event  
  28.  void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)  
  29.  {  
  30.      //Using red pen to draw border  
  31.      using (var redPen = new Pen(Color.Red, 3))  
  32.      {  
  33.          //Get the x coordination value of the left line  
  34.          int left_x = this.dataGridView1.GetCellDisplayRectangle(dataGridView1.Columns["Col_B"].Index, this.dataGridView1.Rows[0].Index, true).X;  
  35.   
  36.          //Get the x coordination value of the right line  
  37.          int right_x = this.dataGridView1.GetCellDisplayRectangle(dataGridView1.Columns["Col_D"].Index, this.dataGridView1.Rows[0].Index, true).X  
  38.                        + this.dataGridView1.GetCellDisplayRectangle(dataGridView1.Columns["Col_D"].Index,this.dataGridView1.Rows[0].Index, true).Width;  
  39.   
  40.          //Get the y coordination value of the top of each line  
  41.          int top_y = this.dataGridView1.GetCellDisplayRectangle(dataGridView1.Columns[0].Index, this.dataGridView1.Rows[0].Index, true).Y;  
  42.   
  43.          //Get the y coordination value of the bottom of each line  
  44.          int bottom_y = this.dataGridView1.GetCellDisplayRectangle(dataGridView1.Columns[0].Index, this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Index, true).Y  
  45.                         + this.dataGridView1.GetCellDisplayRectangle(dataGridView1.Columns[0].Index, this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Index, true).Height;  
  46.   
  47.          //Draw the lines using red pen and the x, y values above  
  48.          e.Graphics.DrawLine(redPen, new Point(left_x, top_y), new Point(left_x, bottom_y));  
  49.          e.Graphics.DrawLine(redPen, new Point(right_x, top_y), new Point(right_x, bottom_y));  
  50.      }  
  51.  }  
 
If I reduce the number of rows to just a few so that all the rows will appear in the form what it first appears, then the red lines appear just as I wanted. But when some rows are hidden, it's screwed up. What am I missing?
Thanks.

Answers (1)