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

查看:346
本文介绍了删除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:


  • ColumnHeadersDefaultCellStyle Alignment 属性设置为 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 或绘制合适的字符:

    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天全站免登陆