GridView根据状态更改底色 为了证实你的猜测,你先改到RowDataBound中试一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1 效率较高的做法是用jquery或者js对行中的status列进行值判断,不同的值加不同的背景色。 大大减轻服务器压力,如果数据很多,不显示的数据不需要去进行处理,只有显示的时候才在客户端处理。2 在前台的绑定中调用方法进行背景色CSS改变。3 如果需要在服务端进行。则可以利用RowDataBound事件进行处理。 很明显你的分页事件不会在执行 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; } 我把代码放在了RowDataBound之后,页面加载也就不显示GridView的绑定了,我再放了绑定代码之后,加载页面就很慢,甚至动不了 Convert.ToString(mydrv["Status"])可以换成 e.Row.Cells[4].Text了,不行的话把RowDataBound里的代码贴出来, 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; } } } } 把代码写成 这样试试: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; }}若不行把你的整个后台代码贴出来 把你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; }我不想误人子弟 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去处理 jquery tree 实现子公司各部门 求指导~ Fckeditor for asp.net的困惑!请asp.net高手指教! 第一次提问!关于vs2005 Using Word的问题 如何才能把GridView列批量转换为编辑状态. 关于一个数据绑定效率的问题 传统的问题关于ASPNET怎样才能学到家 请问怎样用C#弹出对话框,在线等待中 菜鸟问题 mvc3中当前页面js源码加密 搞IT技术到30以后....何去何从? MVC 路由指向 ASP.NET三层架构实现登录功能,数据访问层应该怎么写?
对行中的status列进行值判断,不同的值加不同的背景色。 大大减轻服务器压力,
如果数据很多,不显示的数据不需要去进行处理,只有显示的时候才在客户端处理。2 在前台的绑定中调用方法进行背景色CSS改变。3 如果需要在服务端进行。
则可以利用RowDataBound事件进行处理。
{
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;
}
不行的话把RowDataBound里的代码贴出来,
{
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;
}
}
}
}
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;
}}若不行把你的整个后台代码贴出来
{
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;
}
我不想误人子弟
{
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去处理