Gridview如何根据数据项的值改变颜色? 有个Gridview控件,是用SqlDataSource控件填充数据的,SqlDataSource中有几个数据项.包括IsTop这个数据项,但是IsTop并不在Gridview上显示出来,现在我想根据Istop这个数据项判断是否改变Gridview中某行的颜色,比如IsTop>1 就改变这一行的颜色.不知道能不能做到呢?我考虑过在Gridview的RowDataBound事件入手,但是又不知道怎么实现.各位有什么好办法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的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; } } 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; } } } 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; } } } 大于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; } } } 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; } } } 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; } } } }我修改成这样 动网的那个.net版论坛的好像还是基于.net framework1.1的? C#判断textbox值 讨论数据库访问后的资源释放问题. 如何在页面上动态增加照片及相应的说明 System.Diagnostics.Process.Start为什么没有执行? 错误提示,不明白 后缀为.dll 或.cgi的网页文件是怎么做出来的呢?来者有分,共同探讨! 帮我瞧瞧这段SQL Server的数据库连接有什么错? 数据库连接出错(c#)-dh .net中有没有把字符串转变成日期类型的函数? 获取地址栏中当前文件夹的完整路径 数据库中权限设计!
若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;
}
}
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;
}
}
}
{
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;
}
}
}
{
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;
}
}
}
{
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;
}
}
}
{
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;
}
}
} }
我修改成这样