简体   繁体   English

删除datagridview标题中的排序箭头,并将文本放在框的中心

[英]Remove sorting arrow in datagridview header and put the text in the center of the box

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: 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. p / s:我尝试处理单元格事件绘制并重新绘制headercell中的所有内容,除了.contentbackground箭头消失但文本仍然向左推。 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 - 保持排序箭头,但始终显示它

i am working with vb .net but code in c# is fine 我正在使用vb .net但是c#中的代码很好

how the header is right now 标题现在如何

点击之前的标题

点击后标题

how i want the header to look like 我多么希望标题看起来像

在此输入图像描述

thank you very much 非常感谢你

For aligning column header text at the middle, you can rely on DataGridView properties. 要在中间对齐列标题文本,可以依赖DataGridView属性。 But for custom sort icon, you need custom paint. 但是对于自定义排序图标,您需要自定义绘制。

To set column header text alignment: 要设置列标题文本对齐方式:

  • Set Alignment property of the ColumnHeadersDefaultCellStyle to MiddleCenter . 设置Alignment的财产ColumnHeadersDefaultCellStyleMiddleCenter

To paint custom sort icon: 要绘制自定义排序图标:

  • Handle the CellPainting event and check if we are painting the header: 处理CellPainting事件并检查我们是否正在绘制标题:

     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); 
  • Paint custom sort glyph using DrawImage or by drawing a suitable character: 使用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; 

Note - Draw Visual Styles Sort Icon 注 - 绘制视觉样式排序图标

  • 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)); } 

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

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