简体   繁体   English

如何在Windows Form C#的DataGridView中更改“排序字形图标”颜色?

[英]How to change 'sort glyph icon' color in DataGridView of Windows Form C#?

I've changed the column header color by default. 我默认更改了列标题颜色。 Now, I want to change the 'sort glyph icon' color in DataGridView of Windows Form C# when it gets sorted: 现在,我想在Windows Form C#的DataGridView中更改排序后的'sort glyph icon'颜色:

在此输入图像描述

See the above picture. 见上图。 The column is sorted but icon's color makes it's visibility inadequate. 列已排序,但图标的颜色使其可见性不足。

Please let me know if it's color can be changed. 如果颜色可以改变,请告诉我。 Thanks! 谢谢!

There is no property for changing color of sort icon. 没有用于更改排序图标颜色的属性。 As an option to change it, you can handle CellPainting event and draw the cell yourself. 作为更改它的选项,您可以处理CellPainting事件并自己绘制单元格。

Example

private void dgv1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    var grid = (DataGridView)sender;
    var sortIconColor = Color.Red;
    if (e.RowIndex == -1 && e.ColumnIndex > -1)
    {
        using (var b = new SolidBrush(BackColor))
        {
            //Draw Background
            e.PaintBackground(e.CellBounds, false);

            //Draw Text Default
            //e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground);

            //Draw Text Custom
            TextRenderer.DrawText(e.Graphics, string.Format("{0}", e.FormattedValue),
                e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Left);

            //Draw Sort Icon
            if (grid.SortedColumn?.Index == e.ColumnIndex)
            {
                var sortIcon = grid.SortOrder == SortOrder.Ascending ? "▲":"▼";

                //Or draw an icon here.
                TextRenderer.DrawText(e.Graphics, sortIcon,
                    e.CellStyle.Font, e.CellBounds, sortIconColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }

            //Prevent Default Paint
            e.Handled = true;
        }
    }
}

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

To see drawing with Visual Styles sort icon, take a look at this post . 要查看使用Visual Styles排序图标的绘图,请查看此文章

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

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