Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code for for loop and foreach loop in gridview,
but it don't considered first two rows i.e header and 1 row, first row don't gets colored...

C#
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
         //  foreach (GridViewRow row in GridView1.Rows)
            for (int i = 0; i < GridView1.Rows.Count; i++) 
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                 {

                    if (e.Row.Cells[1].Text == "Pass")
                    {
                        e.Row.BackColor = System.Drawing.Color.LightBlue;
                        }
else
{
  e.Row.BackColor = System.Drawing.Color.Lightpink;
}
}
}
}
Posted
Comments
Laiju k 18-Oct-14 8:51am    
for (int i = 0; i < GridView1.Rows.Count; i++)

instead try for (int i = 0;i<=GridView1.Rows.Count; i++)
Madhuri Gamane 18-Oct-14 9:02am    
got the ans for (int i = 0; i < GridView1.Rows.Count+1; i++)
Rajesh waran 18-Oct-14 9:35am    
Try Solution 1,
if (e.Row.RowType == DataControlRowType.DataRow)
{
for loop here
}

1 solution

 Try this,

          if (e.Row.RowType == DataControlRowType.DataRow)
            {
//Write your code here

            for (int i = 0; i < GridView1.Rows.Count; i++) 
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                 {
 
                    if (e.Row.Cells[1].Text == "Pass")
                    {
                        e.Row.BackColor = System.Drawing.Color.LightBlue;
                    }
                    else
                    {
                        e.Row.BackColor = System.Drawing.Color.Lightpink;
                    }
                 }
             }

           }

Hope This may Help u.
 
Share this answer
 

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

  Print Answers RSS


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