删除 datagridview 标题中的排序箭头并将文本放在框的中心 [英] Remove sorting arrow in datagridview header and put the text in the center of the box

查看:39
本文介绍了删除 datagridview 标题中的排序箭头并将文本放在框的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个需要标题文本位于中心的项目,当单击标题时,它会进行排序.但问题是,有一个排序箭头图标,即使它没有显示,它也会将文本向左推.我想要实现的是

I am working on a project which required header text to be in the center, and when click the header it will do the sorting. But the problem is, there is a sorting arrow icon, even when its not showing it push the text to the left. What i want to achieve is

-去掉排序箭头,将文字居中,但仍保留排序功能

-removing the sorting arrow and put the text to the center but still keep the sorting function

p/s:我尝试处理单元格事件绘制并重新绘制 headercell 中的所有内容,除了 .contentbackground 箭头消失了,但文本仍被推到左侧.这是代码:

p/s: i tried handle cell event paint and repaint everything in headercell except for .contentbackground the arrow gone but the text is still pushed to the left. here is the code:

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex == -1)
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.All &~DataGridViewPaintParts.ContentBackground);

        e.Handled = true;
    }
}

-保留排序箭头但始终显示它

-keep the sorting arrow but always show it

我正在使用 vb .net 但 C# 中的代码很好

i am working with vb .net but code in c# is fine

标题现在如何

我想要标题的样子

非常感谢

推荐答案

要在中间对齐列标题文本,您可以依赖 DataGridView 属性.但是对于自定义排序图标,您需要自定义绘制.

For aligning column header text at the middle, you can rely on DataGridView properties. But for custom sort icon, you need custom paint.

设置列标题文本对齐方式:

To set column header text alignment:

  • ColumnHeadersDefaultCellStyleAlignment 属性设置为 MiddleCenter.
  • Set Alignment property of the ColumnHeadersDefaultCellStyle to MiddleCenter.

绘制自定义排序图标:

  • 处理 CellPainting 事件并检查我们是否正在绘制标题:

  • Handle the CellPainting event and check if we are painting the header:

if (e.RowIndex == -1) //It's header cell

  • 绘制单元格背景

  • Paint cell background

    e.PaintBackground(e.CellBounds, false);
    

  • 绘制内容前景(文本):

  • Paint content foreground (text):

    e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground);
    

  • 使用 DrawImage 或通过绘制合适的字符来绘制自定义排序字形:

  • Paint custom sort glyph using DrawImage or by drawing a suitable character:

    if (grid.SortedColumn?.Index == e.ColumnIndex)
    {
        var sortIcon = grid.SortOrder == SortOrder.Ascending ? "▲":"▼";
    
        //Just for example I rendered a character, you can draw an image.
        TextRenderer.DrawText(e.Graphics, sortIcon,
            e.CellStyle.Font, e.CellBounds, sortIconColor,
            TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
    }
    

  • 停止默认绘制

  • Stop default paint

    e.Handled = true;
    

  • 注意 - 绘制视觉样式排序图标

    • 如果您想绘制默认排序图标:

    • If you wanted to draw default sort icon:

    e.Paint(e.CellBounds, DataGridViewPaintParts.ContentBackground);
    

  • 举个例子,绘制视觉样式排序图标:

  • Just as an example, drawing visual style sort icon:

    if (grid.SortedColumn?.Index == e.ColumnIndex)
    {
        var sortIcon = grid.SortOrder == SortOrder.Ascending ?
            VisualStyleElement.Header.SortArrow.SortedUp : 
            VisualStyleElement.Header.SortArrow.SortedDown;
        var renderer = new VisualStyleRenderer(sortIcon);
        var size = renderer.GetPartSize(e.Graphics, ThemeSizeType.Draw);
        renderer.DrawBackground(e.Graphics,
            new Rectangle(e.CellBounds.Right - size.Width,
            e.CellBounds.Top, size.Width, e.CellBounds.Height));
    }
    

  • 这篇关于删除 datagridview 标题中的排序箭头并将文本放在框的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆