如何在Windows Form C#的DataGridView中更改“排序标志符号图标”的颜色? [英] How to change 'sort glyph icon' color in DataGridView of Windows Form C#?

查看:221
本文介绍了如何在Windows Form C#的DataGridView中更改“排序标志符号图标”的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,我更改了列标题颜色。现在,我想在Windows Form C#的DataGridView中对其进行排序时更改排序标志符号图标的颜色:



事件并自己绘制单元格。



示例

  private void dgv1_CellPainting (对象发送者,DataGridViewCellPaintingEventArgs e)
{
var grid =(DataGridView)sender;
var sortIconColor = Color.Red;
if(e.RowIndex == -1&& e.ColumnIndex> -1)
{
使用(var b = new SolidBrush(BackColor))
{
//绘制背景
e.PaintBackground(e.CellBounds,false);

//绘制文本默认值
//e.Paint(e.CellBounds,DataGridViewPaintParts.ContentForeground);

//绘制自定义文本
TextRenderer.DrawText(e.Graphics,string.Format( {0},e.FormattedValue),
e.CellStyle.Font, e.CellBounds,e.CellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);

//绘制排序图标
if(grid.SortedColumn?.Index == e.ColumnIndex)
{
var sortIcon = grid.SortOrder == SortOrder。上升 ? ▲:▼;

//或在此处绘制图标。
TextRenderer.DrawText(e.Graphics,sortIcon,
e.CellStyle.Font,e.CellBounds,sortIconColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}

//阻止默认绘制
e.Handled = true;
}
}
}

绘制视觉样式排序图标



要查看带有Visual Styles排序图标的图形,请查看此信息


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:

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.

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.

这篇关于如何在Windows Form C#的DataGridView中更改“排序标志符号图标”的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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