GRIDVIEW ROWDATABOUND FOR LOOP NOT WORKING

Barbara Jones

Total Post:13

Points:91
Posted by  Barbara Jones
C#  Gridview 
 1405  View(s)
Ratings:
Rate this:

I have a column 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 below stops after the first section. I want it to loop through all the rows in the 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;
            }
        }
    }

  1. Ankita Pandey

    Post:183

    Points:1285
    Re: GridView RowDataBound For Loop Not Working

    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;
            }

      Modified On Apr-07-2018 12:05:42 AM

Answer

NEWSLETTER

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