2013-03-14 38 views
2

我有一個Windows窗體中的datagridview的工作,我給它分配的負載電網DataSource屬性。我想改變一些單元格的背景顏色(當列索引= 0時),但是當我這樣做時,我調整了窗體的大小,我遇到了問題,數據網格變得模糊不清或者單元格顯示不正確。這些照片會更好地解釋它。Datagrid中變得模糊的調整

調整大小前: 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);    
    } 
} 

提前感謝!

回答

1

的問題是,即使行具有透明背景色。這是因爲您正在使用Color.FromArgb(int argb),並且您將Alpha通道設置爲透明的低值,因此您在重新調整尺寸時單元格的OnBackgrounPaint無法清除背景。更改的最後一行是這樣的:

dg.Rows[e.RowIndex].Cells[0].Style.BackColor = p.AllClients[currentClient].Color; 

如果客戶的財產Color不是Color從GDI +,但一些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 
+0

非常感謝您! – MirlvsMaximvs 2013-03-14 17:16:37

+0

不客氣! – 2013-03-14 17:22:12