for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string i1 = e.Row.Cells[0].Text;
            string i2 = e.Row.Cells[1].Text;
            string i3 = e.Row.Cells[2].Text;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[6].Text == "2008-5-17")
                {
               
                    e.Row.Cells[4].ForeColor= System.Drawing.Color.Green;
                }
            }
        } 

断点测试e.Row.Cells[6].Text 取不到值,为空, 让后前边加了:
   string i1 = e.Row.Cells[0].Text;
            string i2 = e.Row.Cells[1].Text;
            string i3 = e.Row.Cells[2].Text;
就提示指定的参数已超出有效值的范围。
要取的是模板列的值

解决方案 »

  1.   

    如果是取模版列的值,通常的做法是(假设模版中为TextBox)TextBox t = e.Row.FindControl("TextBox1") as TextBox;
    if(t != null)
    {
    string str = t.Text;
    }
      

  2.   

    对是模板列  值我已经取到了  可“指定的参数已超出有效值的范围。 ”又出来了。。  Label t = e.Row.Cells[6].FindControl("updatetime1") as Label;
                if(t != null)
                {
                    string str=t.Text.Substring(0,9);
                
                    for (int i = 0; i < GridView1.Rows.Count; i++)
                    {                    if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            if (str== "2008-5-17")
                            {
                           
                                e.Row.Cells[4].ForeColor= System.Drawing.Color.Green;
                            }
                        }
                    } 
               }
    第一行加断点调试没问题  能取到值了。  可把断点取掉 运行的时候 第一行有错  指定的参数已超出有效值的范围
      

  3.   

    不建议使用 index 方式访问, 这样很容易出错,使用 诸如Label lbl = e.Row.FindControl("MyLabel") as Label; 
    if(lbl != null) { 
    // lbl.Text
    // ...

      

  4.   

    你是在Row_DataBound事件里写的吧?在生成数据网格控件及相关绑定数据时,在加载生成该控件时循环触发该事件,你在事件里写上循环是什么意思呢?