Home > DeveloperSection > Forums > GridView RowDataBound For Loop Not Working
Barbara Jones

Total Post:13

Points:91
Posted on    November-21-2014 12:27 AM

 ASP.Net C#  Gridview 
Ratings:


 1 Reply(s)
 775  View(s)
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;

            }

        }

    }



Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    November-21-2014 10:23 PM

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;

        }


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

Follow MindStick