简体   繁体   中英

Datagrid - how to change color of complete row?

i have a windows-form that contains a datagrid. Currently i have the event "dataGrid_CellFormatting" that checks if the content of a cell contains the word FAIL and the changes the color of this cell to red. This works. What do i have to change that the complete row is changed to red and only the cell?

Thx

    private void dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dataGrid.Columns[e.ColumnIndex].Name.Equals("cResult"))
        {
            if ((String)e.Value == "FAIL")
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
    }

Any reason why you are not just changing all the cells in the row?

private void dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGrid.Columns[e.ColumnIndex].Name.Equals("cResult"))
    {
        if ((String)e.Value == "FAIL")
        {
            foreach (DataGridViewCell cell in dataGrid.Rows[e.RowIndex].Cells)
            {
                cell.Style.BackColor = Color.Red;
            }   
        }
    }
}

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