为了证实你的猜测,你先改到RowDataBound中试一下

解决方案 »

  1.   

    1 效率较高的做法是用jquery或者js
    对行中的status列进行值判断,不同的值加不同的背景色。 大大减轻服务器压力,
    如果数据很多,不显示的数据不需要去进行处理,只有显示的时候才在客户端处理。2 在前台的绑定中调用方法进行背景色CSS改变。3 如果需要在服务端进行。
    则可以利用RowDataBound事件进行处理。
      

  2.   

    很明显你的分页事件不会在执行  if (!IsPostBack){这里的        if (CalStatus == "S")//根据状态状态的代码去更改GridView底色
                    {
                        GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
                    }
                    else if (CalStatus == "A")
                    {
                        GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Green;
                    }
    }底色更改代码,
    GridView1_RowDataBound事件中加入
                    string CalStatus = Convert.ToString(mydrv["Status"]);
                    if (CalStatus == "S")//根据状态状态的代码去更改GridView底色
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
                    }
                    else if (CalStatus == "A")
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Green;
                    }
      

  3.   

    我把代码放在了RowDataBound之后,页面加载也就不显示GridView的绑定了,我再放了绑定代码之后,加载页面就很慢,甚至动不了
      

  4.   

    Convert.ToString(mydrv["Status"])可以换成 e.Row.Cells[4].Text了,
    不行的话把RowDataBound里的代码贴出来,
      

  5.   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userName"] != null)             //判断管理员是否登录
            {
                if (!getSet())                               //判断是否拥有权限  
                {
                    Response.Write("<script>alert('您没有此权限!');location='Default.aspx';</script>");            }
            }
            else
            {
                Response.Redirect("entry.aspx");            //返回到登录页面
            }        
        }    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!IsPostBack)
            {
                string sqlstr = "select * from calMainRecords";
                SqlConnection con = GetConnection();
                SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
                DataSet ds = new DataSet();
                con.Open();
                sda.Fill(ds, "Cal");
                GridView1.DataSource = ds;
                GridView1.DataBind();
                for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                {
                    DataRowView mydrv = ds.Tables["Cal"].DefaultView[i];
                    string CalStatus = Convert.ToString(mydrv["Status"]);
                    DateTime dt = Convert.ToDateTime(mydrv["Cal_Date"]);//对Cal_Date这一列转换成长日期格式
                    GridView1.Rows[i].Cells[2].Text = dt.ToLongDateString();
                    if (CalStatus == "S")
                    {
                        GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;//根据状态来显示GridView的底色
                    }
                    else if (CalStatus == "A")
                    {
                        GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Green;
                    }
                    
                }
            }
        }
      

  6.   

    把代码写成 这样试试:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    string CalStatus = e.Row.Cells[4].Text;
                    if (CalStatus == "S")//根据状态状态的代码去更改GridView底色
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
                    }
                    else if (CalStatus == "A")
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Green;
                    }}若不行把你的整个后台代码贴出来
      

  7.   

    把你GridView1_RowDataBound里的这段代码 if (!IsPostBack)
            {
                string sqlstr = "select * from calMainRecords";
                SqlConnection con = GetConnection();
                SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
                DataSet ds = new DataSet();
                con.Open();
                sda.Fill(ds, "Cal");
                GridView1.DataSource = ds;
                GridView1.DataBind();
                for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                {
                    DataRowView mydrv = ds.Tables["Cal"].DefaultView[i];
                    string CalStatus = Convert.ToString(mydrv["Status"]);
                    DateTime dt = Convert.ToDateTime(mydrv["Cal_Date"]);//对Cal_Date这一列转换成长日期格式
                    GridView1.Rows[i].Cells[2].Text = dt.ToLongDateString();
                    if (CalStatus == "S")
                    {
                        GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;//根据状态来显示GridView的底色
                    }
                    else if (CalStatus == "A")
                    {
                        GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Green;
                    }
                    
                }
            }
    放回到Page_Load事件内,然后把GridView1_RowDataBound事件里的代码写成string CalStatus = e.Row.Cells[4].Text;
                    if (CalStatus == "S")//根据状态状态的代码去更改GridView底色
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
                    }
                    else if (CalStatus == "A")
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Green;
                    }
    我不想误人子弟
      

  8.   

    if (!IsPostBack)
            {
                string sqlstr = "select * from calMainRecords";
                SqlConnection con = GetConnection();
                SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
                DataSet ds = new DataSet();
                con.Open();
                sda.Fill(ds, "Cal");
                GridView1.DataSource = ds;
                GridView1.DataBind();
    }
    把上面那一段放在Page_Load中不要动,DataBind便会触发DataBound事件,所以你那等于是死循环了
    并且去掉for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
    你注意看GridView1_RowDataBound,既然是针对Row的DataBound,那么事件处理中针对的就是某一行,从表头开始,你这里需要跳过
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         //仅当内容行处于正常状态(非编辑状态)时需改变
         if ((e.Row.RowState&DataControlRowState.Normal) == DataControlRowState.Normal && e.Row.RowType == DataControlRowType.DataRow)
         {
               var mydrv = e.Row.DataItem as DataRowView;
               string CalStatus = Convert.ToString(mydrv["Status"]);
                    DateTime dt = Convert.ToDateTime(mydrv["Cal_Date"]);//对Cal_Date这一列转换成长日期格式
                    e.Row.Cells[2].Text = dt.ToLongDateString();
                    if (CalStatus == "S")
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Red;//根据状态来显示GridView的底色
                    }
                    else if (CalStatus == "A")
                    {
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Green;
                    }
              }
         }
    }
    不过要说明的是,2#说的很对,像这种问题用首选都应该是用js去处理