Home > DeveloperSection > Forums > Error in Coloring Datagridview Row in c#
Goti Bandu

Total Post:119

Points:835
Posted on    August-18-2014 3:08 AM

 C# C# 
Ratings:


 1 Reply(s)
 878  View(s)
Rate this:

I have a datageidview which supposed to color the rows with contains specific value

    private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

    {

        foreach (DataGridViewRow myrow in dataGridView2.Rows)

        {

            if (e.RowIndex != -1)

            {

                    if (myrow.Cells[7].Value.ToString() == "Error")

                    {

                        myrow.DefaultCellStyle.BackColor = Color.Red;

                    }

                    else if (myrow.Cells[7].Value.ToString() == "NoError")

                    {

                        myrow.DefaultCellStyle.BackColor = Color.Green;

                    }

                }

        }

    }

but i have a problem when the first row contains this value all the rows is colored with it's color

any help ??



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-18-2014 3:32 AM

Hi Goti, 

The CellFormatting event is sent for all visible cells in the grid. You may have better luck using the data given in the event to change the color.

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

    if (e.RowIndex != -1)

    {

        if (dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString() == "Error")

        {

            e.CellStyle.BackColor = Color.Red;

        }

        else if (dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString() == "NoError")

        {

             e.CellStyle.BackColor = Color.Green;

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick