c# - How to make transparent alpha color of a cell datagridview?


Keywords:c# 


Question: 

I have a datagridview as below:

enter image description here

I would like to make background color of a cell datagridview about 20%, 50%,... transparent (not fully transparent), where there is a color in the background (i.e. Black, Purple, Gray,...)?

I have tried many ways but I am not able to do it:

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

Any tips on these will be great help. Thanks in advance.


1 Answer: 

After refer to the link below, I did it run well.

I hope this is useful for anyone required!