【发布时间】:2017-02-19 01:31:04
【问题描述】:
我有一个如下的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();
}
任何关于这些的提示都会有很大帮助。提前致谢。
【问题讨论】:
-
您正在使用的是 WPF 还是基本表单?
-
抱歉没有解释清楚。这是一个基本的窗体,windows窗体应用程序。
-
你试过 Color.FromArgb(whiteness, color) 吗? msdn.microsoft.com/pt-br/library/1hstcth9(v=vs.110).aspx
-
我试过了但是不行:(
标签: c# datagridview cell background-color transparent