GridView RowDataBound For循环不起作用

时间:2014-10-05 16:42:40

标签: c# asp.net gridview

我有一个看起来像这样的列

1 1 五 1 1 1 五 1 1 1 2 1
2 3 1 1 1 1 3

我想要做的是突出显示5到5之间的所有内容以及2到2之间的所有内容等等。但是下面的脚本在第一部分之后停止。我希望它遍历网格视图中的所有行。

[代码]

    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 个答案:

答案 0 :(得分:0)

像这样的工作?

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