---
title: "GridView RowDataBound For Loop Not Working"  
description: "GridView RowDataBound For Loop Not Working"  
author: "Barbara Jones"  
published: 2014-11-21  
updated: 2014-11-21  
canonical: https://www.mindstick.com/forum/12667/gridview-rowdatabound-for-loop-not-working  
category: "asp.net"  
tags: ["c#", "gridview"]  
reading_time: 2 minutes  

---

# GridView RowDataBound For Loop Not Working

I have a [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) that looks like this

1 1 5 1 1 1 5 1 1 1 2 1\
2 3 1 1 1 1 3

What I want to do highlight everything between 5 and 5 and everything between 2 and 2 etc... However the [script](https://www.mindstick.com/interview/477/how-can-i-execute-a-php-script-using-command-line) below stops after the first section. I want it to loop through all the rows in the [grid view](https://www.mindstick.com/forum/477/grid-view).

```
[code]    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        for (int i = 0; i < 1; i++)        {            bool begin = false;            foreach (GridViewRow row in GridView1.Rows)            {                if (row.Cells[i + 2].Text != "1")                {                    row.Cells[i + 2].BackColor = System.Drawing.Color.Blue;                    if (!begin) begin = true;                    else                        break;                }                if (begin) row.Cells[i + 2].BackColor = System.Drawing.Color.Blue;            }        }    }
```

## Replies

### Reply by Anonymous User

something like this work?

```
 GridViewRow row = e.Row;                    int firstindex= 0;        int Lastindex =0;        if (e.Row.RowType== DataControlRowType.DataRow)        {            string value = e.Row.Cells[0].Text;              
            firstindex = value.IndexOf('5');            Lastindex =value.LastIndexOf('5');            string refinedVal = value.Substring(0, firstindex);            string refinedVal1 = value.Substring(firstindex + 1, Lastindex);            string refinedVal2 = value.Substring(Lastindex);            string finalop = refinedVal + "<span style='color:blue'>" +refinedVal1 + @"</span>" + refinedVal2;            row.Cells[0].Text= string.Empty;            row.Cells[0].Text= finalop;        }
```


---

Original Source: https://www.mindstick.com/forum/12667/gridview-rowdatabound-for-loop-not-working

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
