Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a userEventGrid_CellFormatting Event to change the cell back color when a specific text exists in a particular column and its working fine.
however if i set to Row back color instead of cell back color it doesnt work, where all the rows back color in gridview will be changed instead of specific rows only.
Please help me, what i am doing wrong...?

What I have tried:

code to change cell back color (working fine)

private void usrEventGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.usrEventGrid.Columns[e.ColumnIndex].Name == "Update")
    {

        if (e.Value != null)
        {
            string stringValue = (string)e.Value;
            if (stringValue == "1A")
            {
                int rowIndex = (int)e.RowIndex;
                e.CellStyle.BackColor = Color.Pink;
            }
        }
    }


}



to highlight entire row i changed the above code from

e.CellStyle.BackColor = Color.Pink;


to

usrEventGrid.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Pink;



however the whole datagirdview is highlighted instead of specific row, please help
Posted
Updated 24-Jan-20 4:57am

1 solution

Well CellFormatting will be fired for each cell which needs to be painted.

First thing you need to do is to check the row and set the highlight color if you wish.

Second, do not forget to set the color back to "normal" in case you don't need it highlighted and in case you really want to control this by DefaultCellStyle

Conclusion: Hightlight the cell/row by checking the row index and do use e.CellStyle.BackColor instead of DefaultCellStyle. Than you don't need to set the color back to "normal"
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900