【问题标题】:Datagrid gets blurred on resizingDatagrid 在调整大小时变得模糊
【发布时间】:2013-03-03 03:21:17
【问题描述】:

我正在使用 Windows 窗体中的数据网格视图,并为其分配数据源属性以加载网格。我想更改某些单元格的背景色(当列索引 = 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);              
     } 
}   

提前致谢!

【问题讨论】:

    标签: c# winforms datagrid


    【解决方案1】:

    问题是即使行也有透明的背景颜色。这是因为您正在使用 Color.FromArgb(int argb) 并且您将 alpha 通道设置为透明的低值,因此您的单元格 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
    

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多