简体   繁体   English

DataGridView标头上的自定义标志符号

[英]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. 我有一个DataGridView ,当用户单击它时,我想在列标题上绘制自定义标志符号(假设为三角形)。

I have the property EnableHeadersVisualStyles set to False . 我将属性EnableHeadersVisualStyles设置为False

Do you have any example or suggestions how to reach the desired result? 您是否有任何示例或建议如何达到预期效果? Do I need to inherit from DataGridView, or DataGridViewColumn? 我是否需要从DataGridView或DataGridViewColumn继承?

Thank you in advance, Marco 预先感谢您,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". 将列的SortMode属性设置为“ 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? 来自: 如何将标题居中在DataGridView的列中?

you need to do a few thing first attach handler to event GridView_CellPainting the inside the event handler do like this 您需要做一些事情,首先将处理程序附加到事件GridView_CellPainting,然后在事件处理程序中执行以下操作

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;" 行“ sf.Alignment = StringAlignment.Near;” set text to be painted left alignemet if you need it otherwise change 如果需要,将文本设置为左对齐

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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