如何制作单元格datagridview的透明alpha颜色?

时间:2016-10-10 15:14:08

标签: c# datagridview cell background-color transparent

我有一个datagridview如下:

enter image description here

我想制作一个单元格datagridview的背景颜色大约20%,50%,...透明(不完全透明),背景中有一种颜色(即黑色,紫色,灰色,...... )?

我尝试了很多方法,但我无法做到:

private void CustomerDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        // Black 20%
        if (e.ColumnIndex == 1)
        {
            SetCellsTransparent(e.RowIndex, Color.Black, 0.2);
        }

        // Purple 50%
        if (e.ColumnIndex == 2)
        {
            SetCellsTransparent(e.RowIndex, Color.Purple, 0.5);
        }

        // Gray 100%
        if (e.ColumnIndex == 3)
        {
            SetCellsTransparent(e.RowIndex, Color.Gray, 1);
        }
    }
    catch (Exception) { }
}

public void SetCellsTransparent(int rowIdx, Color color, double transparency)
{
    //Color color = ColorTranslator.FromHtml("#000000");
    //Color color = Color.Red;
    //this.Rows[rowIdx].Cells[0].Style.BackColor = color;

    //this.Rows[rowIdx].Cells[0].Style.BackColor = Color.FromArgb(50, color);

    var whiteness = 255 - Convert.ToInt32(transparency * 255);
    this.Rows[rowIdx].Cells[0].Style.BackColor = Color.FromArgb(255, whiteness, whiteness); //Red

    this.ClearSelection();
}

任何有关这些的提示都会有很大帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在参考下面的链接后,我做得很好。 http://www.codeproject.com/Articles/6502/Transparency-Tutorial-with-C-Part

我希望这对任何人都有用!