DataGridView changing cell background color

2020-01-27 06:46发布

I have the following code :

private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in dgvStatus.Rows)
    {
        row.Cells[color.Index].Style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
    }
}

I am trying to set the background color of each cell from the background color column. this doesn't work the color never change. Any idea of why?

I've been looking around but didn't found anything usefull

10条回答
Anthone
2楼-- · 2020-01-27 07:10
protected void grdDataListeDetay_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text != "0")
        {
            for (int i = 0; i <= e.Row.Cells.Count - 1; i++)
            {
                e.Row.Cells[i].BackColor = System.Drawing.Color.Beige;
            }
        }
    }
}
查看更多
啃猪蹄的小仙女
3楼-- · 2020-01-27 07:12
dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.LightGreen;
查看更多
够拽才男人
4楼-- · 2020-01-27 07:17
dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;
查看更多
倾城 Initia
5楼-- · 2020-01-27 07:18

try the following (in RowDataBound method of GridView):

protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // this will only change the rows backgound not the column header 

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.LightCyan; //first col
        e.Row.Cells[1].BackColor = System.Drawing.Color.Black; // second col
    }
}
查看更多
登录 后发表回答