c# - Datagrid 在调整大小时变得模糊

标签 c# winforms datagrid

我正在使用 Windows 窗体中的数据 GridView ,并为其分配数据源属性以加载网格。我想改变一些单元格的背景色(当列索引=0时)但是 当我这样做并调整表格大小时我遇到了问题,数据网格变得模糊或单元格未正确显示。这些图片会更好地解释它。

调整大小之前: enter image description here

调整大小后: enter image description here

这是我尝试格式化单元格的代码...

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/

相关文章:

c# - 如何在 Windows 移动应用程序中存储字符串值并在以后检索它

c# - 我可以告诉 C# 可空引用方法实际上是对字段的空检查吗

c# - 反序列化时仅用于未设置属性的默认值

c# - WPF MVVM Bind Dictionary<String, List<String>> 到数据网格

c# - 一种从 DataRowView 获取 DataRow 的方法

c# - 将嵌套字典转换为 IReadOnlyDictionary

c# - TCPListener 在 listener.EndAcceptTcpClient(asyncResult) 上抛出 Socket 异常

c# - 尺寸改变时宽度不改变

c# - 在 WinForms 桌面应用程序中使用 jQuery?

wpf - 将绑定(bind)数据传递到 DataGrid 中的用户控件