【问题标题】:Custom glyph on DataGridView HeaderDataGridView Header 上的自定义字形
【发布时间】:2010-02-16 09:46:14
【问题描述】:

我有一个 DataGridView,当用户单击它时,我想在列标题上绘制自定义字形(假设是一个三角形)。

我将属性 EnableHeadersVisualStyles 设置为 False

您对如何达到预期结果有任何示例或建议吗?我需要从 DataGridView 或 DataGridViewColumn 继承吗?

提前谢谢你, 马可

【问题讨论】:

    标签: .net vb.net datagridview


    【解决方案1】:

    您需要做一些事情,首先将处理程序附加到事件 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;
                }
            }
    

    行“sf.Alignment = StringAlignment.Near;”如果需要,将文本设置为左对齐,否则更改

    【讨论】:

      【解决方案2】:

      我发现这很有帮助。效果很好

      如果您希望列标题完全居中,您需要 禁用排序。将列的 SortMode 属性设置为 “不可排序”。这应该防止空间被保留给 只要列文本居中或右对齐,就对字形进行排序。

      发件人:How can I center the heading in a column on a DataGridView?

      【讨论】:

        猜你喜欢
        • 2010-11-25
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 2010-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多