【问题标题】:Transparent SelectionBackColor for DataGridView CellDataGridView单元格的透明选择背景色
【发布时间】:2016-07-12 19:58:12
【问题描述】:

在 Windows 窗体 DataGridView 中,我试图使所选行具有粗体字体并且不更改背景颜色。

为此,我使用了以下代码:

// This method is used to load the data into the grid
private void GridLoad()
{
    // Configures the Data Source

    Grid.DefaultCellStyle.SelectionBackColor = Color.Transparent;

    Grid.ClearSelection();          
}

private void Grid_SelectionChanged(object sender, EventArgs e)
{
    var dataGridView = Grid;

    foreach (DataGridViewRow row in dataGrid.Rows)
    {
        row.DefaultCellStyle.Font = dataGrid.Font;
    }

    if (dataGridView.SelectedRows.Count > 0)
    {
        var selectedRow = dataGridView.SelectedRows[0];
        selectedRow.DefaultCellStyle.Font = new Font(dataGridView.Font, FontStyle.Bold);
    }            
}

代码有效,当我单击其中一行选择它时,字体变为粗体,但有一个重叠。

文本被重复,原来的常规字体似乎停留在背景上,而新的粗体文本出现在顶部略微向右偏移。

为什么会这样?为什么会发生这种重叠,我该如何解决?

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    主要问题出在这一行:

    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Transparent;
    

    删除它,然后您将在渲染中没有问题。

    不要将SelectionBackColor 设置为Color.Transparent,如果你想要一个987654326@的单元格。

    更适合这种目的的事件是DataGridViewCellFormatting 事件。使用此事件,您可以为单元格提供动态格式:

    void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex >= 0 && e.RowIndex >= 0 &&
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
        {
            e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
            e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor;
            e.CellStyle.SelectionBackColor = e.CellStyle.BackColor;
        }
        else
        {
            e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);
        }
    }
    

    【讨论】:

    • 感谢您的回答。这也有效,它正确地使所选行的字体变粗,但仍然存在重叠问题。你有任何猜测它可能是什么?再次感谢!
    • 检查编辑的答案,主要问题在这一行:Grid.DefaultCellStyle.SelectionBackColor = Color.Transparent;。删除它,然后您将在渲染中没有问题。如果您不想拥有SelectionBackColor,只需将SelectionBackColor 设置为单元格的BackColor 的值即可。
    • 感谢您的回答和解决方案!另外,很抱歉延迟将答案标记为已接受,我忘记了。
    【解决方案2】:

    由于我也在寻找不覆盖原始颜色并且找不到任何好的答案,因此我对此进行了扩展以创建半透明的外观。 如果背景颜色发生变化,它会保持最新状态,并且还可以选择单个单元格

    你可以像这样使用这个扩展

    dataGridView1.EnableSemiTransparentSelection();
    

    这将是结果

    首先你需要一个混色器,如果我们使用原始背景颜色,这将创建透明效果

        //Mix two colors
        //Example: Steps=10 & Position=4 makes Color2 mix 40% into Color1
        public static Color MixColor(Color Color1, Color Color2, int Steps, int Position)
        {
            if (Position <= 0 || Steps <= 1) { return Color1; }
            if (Position >= Steps) { return Color2; }
            return Color.FromArgb(
                Color1.R + ((Color2.R - Color1.R) / Steps * Position),
                Color1.G + ((Color2.G - Color1.G) / Steps * Position),
                Color1.B + ((Color2.B - Color1.B) / Steps * Position)
                );
        }
    

    二、制作扩展

        //Mix SelectionBackColor
        public static void EnableSemiTransparentSelection(this DataGridView dataGridView)
        {
            dataGridView.RowPrePaint += new DataGridViewRowPrePaintEventHandler(GridSemiTransparentSelection_RowPrePaint);
        }
        private static void GridSemiTransparentSelection_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;
            foreach (DataGridViewCell cell in dataGridView.Rows[e.RowIndex].Cells)
            {
                if (cell.Selected == false) { continue; }
                var bgColorCell = Color.White;
                if (cell.Style.BackColor != Color.Empty) { bgColorCell = cell.Style.BackColor; }
                else if (cell.InheritedStyle.BackColor != Color.Empty) { bgColorCell = cell.InheritedStyle.BackColor; }
                cell.Style.SelectionBackColor = MixColor(bgColorCell, Color.FromArgb(0,150,255), 10, 4);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2014-10-08
      • 2013-04-12
      • 2014-02-27
      • 2016-05-27
      • 2015-07-11
      相关资源
      最近更新 更多