Datagridview hiding rowheader record selectors

Giuseppe PizzutoIT Director
CERTIFIED EXPERT
Published:
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions.
Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column.

First solution, the easy way
The first solution is to make the column slim enough, so that the record selectors are not drawn: the are on the left side of the column. Set the property RowHeaderWidth to a value less than 20, but this value depends on the style you used...

Second solution, the long way
OK, you can always redraw your items completely, by doing the work "by hand"...
This is what I found by Googling; it is in C# language, but I think it's easy to understand.

 void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs  e)
                      
                              {
                      
                                  if (e.ColumnIndex == -1 && e.RowIndex > -1)
                      
                                  {
                      
                                      e.PaintBackground(e.CellBounds,true);
                      
                                      using (SolidBrush br = new SolidBrush(Color.Black))
                      
                                      {
                      
                                          StringFormat sf = new StringFormat();
                      
                                          sf.Alignment = StringAlignment.Center;
                      
                                          sf.LineAlignment = StringAlignment.Center;
                      
                                          e.Graphics.DrawString(e.RowIndex.ToString(),
                      
                                              e.CellStyle.Font, br, e.CellBounds, sf);
                      
                                      }
                      
                                      e.Handled = true;
                      
                                  }
                      
                              }

Open in new window


Third solution, the smart way

I prefer this solution. I don't know if anyone (and it is probable) have already posted something similar, in this case I apologize :-)
This solution is the middle-way of the others. It is simplier and doesn't need to draw anything: just ask the datagridview to do the job for you.

Private Sub GRD_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles GRD.CellPainting
                              If (e.ColumnIndex < 0) AndAlso (e.RowIndex >= 0) Then
                                  ' This will paint the background
                                  e.PaintBackground(e.CellBounds, False)
                                  ' And this will paint your text without the record selectors
                                  e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground)
                                  ' This avoids grid's own paint handler to execute
                                  e.Handled = True
                              End If
                          End Sub

Open in new window


In this way, you don't have to set the text fonts/format/colors and the backgrounds.
The content of the first column is painted correctly (if you need, for example a long description for the row).
I hope this would help developers when they spend time in "tuning" applications.
0
11,086 Views
Giuseppe PizzutoIT Director
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.