ERROR IN COLORING DATAGRIDVIEW ROW IN C#

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
C# 
C#
 1137  View(s)
Ratings:
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 ??

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Error in Coloring Datagridview Row in c#

    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;

            }

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!