<

Datagridview hiding rowheader record selectors

Published on
15,645 Points
9,645 Views
Last Modified:
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
Comment
Author:gpizzuto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month