有个Gridview控件,是用SqlDataSource控件填充数据的,SqlDataSource中有几个数据项.包括IsTop这个数据项,但是IsTop并不在Gridview上显示出来,现在我想根据Istop这个数据项判断是否改变Gridview中某行的颜色,比如IsTop>1 就改变这一行的颜色.不知道能不能做到呢?我考虑过在Gridview的RowDataBound事件入手,但是又不知道怎么实现.各位有什么好办法吗?

解决方案 »

  1.   

    你的IsTop的项必须是模板列,visible设置为false;
    若IsTop的模板列名称为Label1,并且在第5列,则代码如下:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Convert.ToInt32(((Label)e.Row.Cells[5].FindControl("Label1")).Text) > 1)
                    e.Row.BackColor = System.Drawing.Color.Red;
            }
        }
      

  2.   


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                if (drv.Row["IsTop"].ToString() == "1")
                {
                    e.Row.ForeColor = System.Drawing.Color.IndianRed;
                }
            }
        }
      

  3.   

    for(int i = 0; i <= GridView1.Rows.Count - 1; i++) 
            { 
            DataRowView dr= ds.Tables[0].DefaultView[i]; 
              if (dr[""].ToString.Equals("100")) 
                { 
                    GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Red; 
                } 
            } 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {            Label labValue = (Label)e.Row.FindControl("lab");
                if (labValue.Text == "100")
                {
                    e.Row.BackColor = System.Drawing.Color.Red;
                 }
                if (labValue.Text == "200")
                {
                    e.Row.BackColor = System.Drawing.Color.Yellow;
                  }
            }
        }
      

  4.   

    大于1啊,改一下    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                if (Convert.ToInt32(drv.Row["IsTop"]) > 1)
                {
                    e.Row.ForeColor = System.Drawing.Color.IndianRed;
                }
            }
        }
      

  5.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                if (Convert.ToInt32(drv.Row["IsTop"]) > 1)
                {
                    e.Row.ForeColor = System.Drawing.Color.IndianRed;
                }
            }
        }
      

  6.   

    public void SetColor(GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 8; i < 11; i++)
                {
                    if (e.Row.Cells[i].Text == "否")
                    {
                        e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
                    }
                }
              
            }     }
    我修改成这样