我正在使用 Windows 窗体中的数据 GridView ,并为其分配数据源属性以加载网格。我想改变一些单元格的背景色(当列索引=0时)但是 当我这样做并调整表格大小时我遇到了问题,数据网格变得模糊或单元格未正确显示。这些图片会更好地解释它。
调整大小之前:
调整大小后:
这是我尝试格式化单元格的代码...
private void dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// Clients color
if (e.ColumnIndex == 0)
{
int currentClient = e.RowIndex % p.AllClients.Count;
dg.Rows[e.RowIndex].Cells[0].Style.BackColor = Color.FromArgb(p.AllClients[currentClient].Color);
}
}
提前致谢!
最佳答案
问题是即使是行也有透明的背景色。这是因为您正在使用 Color.FromArgb(int argb)
并且您将 alpha channel 设置为一个透明的低值,因此您可以在单元格的 OnBackgrounPaint
调整大小时清除背景。像这样更改最后一行:
dg.Rows[e.RowIndex].Cells[0].Style.BackColor = p.AllClients[currentClient].Color;
如果客户端的属性 Color
不是来自 GDI+ 的 Color
而是一些 32 位数字,您可以这样做:
dg.Rows[e.RowIndex].Cells[0].Style.BackColor = Color.FromArgb(p.AllClients[currentClient].Color);
Color newColor = dg.Rows[e.RowIndex].Cells[0].Style.BackColor;
dataGridView1.Rows[e.RowIndex].Cells[0].Style.BackColor = Color.FromArgb(255, newColor.R, newColor.G, newColor.B); //remove transparency from the color
关于c# - Datagrid 在调整大小时变得模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15414994/