我的Gridview <asp:GridView ID="GridView1" align="center" AutoGenerateColumns="False" runat="server" 
            CellPadding="4" ForeColor="#333333" GridLines="None" 
            onrowdatabound="GridView1_RowDataBound" Width="100%"  AllowPaging="True" 
                            onpageindexchanging="GridView1_PageIndexChanging" AllowSorting="True">
            <RowStyle BackColor="#E3EAEB" />
        <Columns>
                  <asp:TemplateField HeaderText="项目编号">
                          <ItemTemplate>
                          
                          <a href ="project.aspx?id=<%#DataBinder.Eval(Container.DataItem,"p_num")%>" target="_self"><%#DataBinder.Eval(Container.DataItem,"p_num")%></a>
                          </ItemTemplate>
                          </asp:TemplateField>
                <asp:BoundField DataField="p_year" HeaderText="项目年度" >
                  <ItemStyle HorizontalAlign="Center" Width="10%"/>
              </asp:BoundField>
                          
              <asp:BoundField DataField="p_name" HeaderText="项目名称" >
                  <ItemStyle HorizontalAlign="Center" Width="30%"/>
              </asp:BoundField>              <asp:BoundField DataField="p_schedule" HeaderText="项目进度" >
                  <ItemStyle HorizontalAlign="Center" Width="15%"/>
              </asp:BoundField>
              
              <asp:BoundField DataField="p_chief" HeaderText="负责人"  >
                  <ItemStyle HorizontalAlign="Center" Width="15%"/>
              </asp:BoundField>
              
              <asp:BoundField DataField="p_materialiy" HeaderText="重要度"  >
                  <ItemStyle HorizontalAlign="Center" Width="13%"/>
              </asp:BoundField></Columns>
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
为什么在RowDataBound中
e.row.cells[3].text超出范围,我的Gridview很多列呀
e.row.cells.count 计算出来为1呢?
我是想根据判断项目进度这一列的值,让一些行显示特殊颜色的
谁能给出方法和代码呀

解决方案 »

  1.   


    //RowDataBound事件
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       //想要的结果  是这么写的么?
    }
      

  2.   

    去找下GridView72绝技里就有这样的介绍了。
      

  3.   

    先申明一下,没有看你的问题,但是对于Gridview,你可以使用JqGrid,功能超级强大,个人感觉比Gridview强多了,
      

  4.   

    有没有点有用的回复啊...要根据列的值改变行的颜色。列的值的string类型
      

  5.   


    楼主 你的问题是超出索引范围,我给的代码能解决你的索引范围出错的问题你想要根据值切换行的颜色还不简单?
    //RowDataBound事件
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       //想要的结果  是这么写的么?
       //这里根据值判断
       if(e.row.cells[3].text == "你说的值")
       {
          e.Row.Attributes.Add("style","backgroundColor:颜色;");
       }
    }
      

  6.   

    if(e.row.cells[3].text == "你说的值")
       {
          e.Row.Attributes.Add("style","backgroundColor:颜色;");
       }
    没测试 你自己改改
      

  7.   

    我是这样写的啊。
    看我的提问!为什么在RowDataBound中e.row.cells[3].text超出范围,我的Gridview很多列呀我又用这个统计了下e.row.cells.count 计算出来怎么是1 呢?
      

  8.   

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       //代码
    }if判断不是解决了索引超出范围?
      

  9.   


    RowDataBound里头他会把header和footer包括在内,这两个就是只有1列,你应该用楼上的代码
      

  10.   

    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string  schedule = e.Row .Cells [3].Text ;
                if (schedule  == "5.确认")
                {
                    e.Row.BackColor = System.Drawing.Color.Blue;
                }
            }
    这样写的话,没有变色。把if (e.Row.RowType == DataControlRowType.DataRow)去掉  就会索引超出范围了该咋办啊....
      

  11.   


    代码没错,怎么会没变色?你确认e.Row .Cells [3].Text的值是"5.确认"???
      

  12.   

    你看我上面的gridview  第四列不就是 项目进度    
    我想让项目进度为5.确认的行为其他颜色
    不就应该这样写么?
    string schedule = e.Row .Cells [3].Text ;
    if (schedule == "5.确认")
    {
    e.Row.BackColor = System.Drawing.Color.Blue;
    }
      

  13.   


    断点调试if (schedule == "5.确认")
    {
    }里面执行没?
    你是怎么写值的哦 项目进度 比如可以分为:  
    确认
    完成
    修改干嘛用
    1.确认
    2.完成
    ....
      

  14.   

    按设计课的要求做的进度
    断点这个schedule值为空呢
    想不明白。gridview中 第4列 不就是 项目进度   不就是cells[3]呀
      

  15.   

    数据库 表 项目进度字段 p_schedule 有没有值哦?
      

  16.   

    成功了我改成这样了
         string schedule = e.Row.Cells[3].Text.ToString ().Trim ();
    加了个去除空格。
    谢谢上面那位一直解释了...