簡體   English   中英

如何在Windows Form C#的DataGridView中更改“排序字形圖標”顏色?

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

我默認更改了列標題顏色。 現在,我想在Windows Form C#的DataGridView中更改排序后的'sort glyph icon'顏色:

在此輸入圖像描述

見上圖。 列已排序,但圖標的顏色使其可見性不足。

如果顏色可以改變,請告訴我。 謝謝!

沒有用於更改排序圖標顏色的屬性。 作為更改它的選項,您可以處理CellPainting事件並自己繪制單元格。

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

繪制視覺樣式排序圖標

要查看使用Visual Styles排序圖標的繪圖,請查看此文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM