简体   繁体   中英

Datagrid gets blurred on resizing

I'm working with a datagridview within a windows form and I assign it's datasource property for load the grid. I want to change the backcolor of some cells (when column index=0) but when i do this and i resize the form i have a problem, the datagrid gets blurred or the cells aren't showed correctly. These pics will explain it better.

Before resize: 在此处输入图片说明

After resize: 在此处输入图片说明

Here is my code where i'm trying to format the cells...

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);              
     } 
}   

Thanks in advance!

The problem is that even rows have transparent background color. This is because you are using Color.FromArgb(int argb) and you are setting alpha channel to a low value which is transparent and therefore you're OnBackgrounPaint of the cell can't clear the background when re-sizing. Change the last line like this:

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

if the property Color of the client is not the Color from GDI+ but some 32bit number you could do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM