<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Datagridview hiding rowheader record selectors

Published on
16,299 Points
10,299 Views
Last Modified:
Approved
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
Author:gpizzuto
0 Comments

Featured Post

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Overview of OneDrive and collaboration.
Make it easier to see the current record on your Microsoft Access forms! To highlight the current record with a yellow background color, use Conditional Formatting, a control to keep track of the primary key value, a control to change color, and a l…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month