简体   繁体   中英

Custom glyph on DataGridView Header

I have a DataGridView where I would like drawing custom glyph (let's suppose a triangle) on the column header when the user click on it.

I have the property EnableHeadersVisualStyles set to False .

Do you have any example or suggestions how to reach the desired result? Do I need to inherit from DataGridView, or DataGridViewColumn?

Thank you in advance, Marco

I have found this helpful. It works perfectly

If you want the column headers to be perfectly centered, you'll need to disable sorting. Set the SortMode property for the column to "NonSortable". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.

From: How can I center the heading in a column on a DataGridView?

you need to do a few thing first attach handler to event GridView_CellPainting the inside the event handler do like this

void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
                e.PaintBackground( e.ClipBounds, true );
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                e.Graphics.DrawString( e.FormattedValue.ToString() this.ColumnHeadersDefaultCellStyle.Font, new SolidBrush( this.ColumnHeadersDefaultCellStyle.ForeColor ),e.CellBounds, sf );

                             //
                             //Place code to draw your custom gliph
                             //
                e.Handled = true;
            }
        }

the row "sf.Alignment = StringAlignment.Near;" set text to be painted left alignemet if you need it otherwise change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM