GRIDVEIW 特定的单元格显示红色
private void BindData( ){
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ConnectionString = ConfigurationSettings.AppSettings["connStr"];
SqlDataSource1.SelectCommand = "select batid,CellRcs,cdmabattery.CellName CellName,BatteryFac,BatteryType,BatteryNum,SupplyHour,IsUsing,BatteryDepart,DutyMan,ProjectName,BatOther,SetDatetime,Adddate,area from cdmabattery,jzcdmacell where cdmabattery.cellrcs=jzcdmacell.cellnumber order by batid desc";
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
AspNetPager2.RecordCount = dv.Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager2.CurrentPageIndex - 1;
pds.PageSize = AspNetPager2.PageSize;
GridView1.DataSource = pds;
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRow mydrv = dv.Table.Rows[i];
//DataRowView mydrv = myds.Tables["Admin"].DefaultView[i]; table
string hour = Convert.ToString(mydrv["SupplyHour"]);
if (Convert.ToDouble(hour) < 4.00)//这里根据具体情况设置可能ToInt32等等
{
//GridView1.Rows[i].BackColor = System.Drawing.Color.Red; //行显示红色
GridView1.Rows[i].Cells[6].BackColor = System.Drawing.Color.Red; //列显示红色
}
}
}
protected void AspNetPager2_PageChanged(object src, EventArgs e)
{
GridView1.EditIndex = -1;
BindData( );
}
当 supplyhour《4.00时,显示红色,但是翻页时显示就不对了,如果第一页的第2行第7列显示红色,等到翻页时第2、3、4、的第2行第7列也显示红色(即使supplyhour》4.00也是这样),不知道是怎么回事,还请各位大侠指点!!!!谢谢!!分页控件是ASPNETPAGER.. --------------------编程问答-------------------- 自己顶一个!!!!!!!!在线等!!!!!!!!! --------------------编程问答-------------------- protected void GridView1_DataBound(object sender, EventArgs e)
{//某列不同数值显示不同颜色
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (Convert.ToInt32( GridView1.Rows[i].Cells[2].Text.ToString()) < 100)
{
GridView1.Rows[i].Cells[2].ForeColor = System.Drawing.Color.Red;
}
else
{
GridView1.Rows[i].Cells[2].ForeColor = System.Drawing.Color.Black;
}
}
} --------------------编程问答-------------------- 模版列中<# Eval("").ToString().Equals("")?"<font color=''>"+Eval("").ToString() +"</font>":("")?"<font color=''>"+Eval("").ToString() +"</font>"%>
或设置<%# GetColor(Eval("a").ToString(),Eval("B").ToString(),(GridViewRow)Container)%>
public void GetColor(string ,string b,GridViewRow row)
{
row.BackColor = Color.Red;
}
或
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowIndex <= 2) || (e.Row.RowIndex >= 6 & e.Row.RowIndex <= 8 ) )
e.Row.BackColor = System.Drawing.Color.Blue;
else
e.Row.BackColor = System.Drawing.Color.White;
}
} --------------------编程问答-------------------- 在GridView.RowDataBound这个事件里写。
hour设置全局变量
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (Convert.ToDouble(hour) < 4.00)
{
e.Row.Cells[6].CssClass = "cellStyle";
}
}
}
前台页面加上个CSS
<style type="text/css"> .cellStyle { background-color:Maroon; color:White; font-weight:bold; } </style>
或者这样 在绑定的时候变色
hour在后台也要全局 或者数据库里的字段 自己知道了思路 随便怎么改
<%# Convert.ToDouble(Eval("hour")) < 4.00 ? "<span style='color:Red'>" + Eval("字段名").ToString() + "</span>": Eval("字段名").ToString() %>--------------------编程问答-------------------- 好像都不行啊????? --------------------编程问答-------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (myfunc.StrToFloat(GridView1.Rows[i].Cells[5].Text.ToString()) < 4)
{
GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.Red;
}
else
{
GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.Black;
}
}
}
怎么显示的都是红色呢?????? --------------------编程问答--------------------
是不是第六列 全都是红色?
断点 看这个myfunc.StrToFloat(GridView1.Rows[i].Cells[5].Text.ToString()) < 4
这个条件。 --------------------编程问答-------------------- 是啊 ,都是红色!!! --------------------编程问答-------------------- 怎么回事啊? --------------------编程问答--------------------
--------------------编程问答--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex == -1)
{
return;
}
if (e.Row.Cells[0].Text.Trim() != " ")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
}
可以吧
--------------------编程问答-------------------- 引用 4 楼 wxr0323 的回复:
在GridView.RowDataBound这个事件里写。
hour设置全局变量
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (myfunc.StrToFloat(DataBinder.Eva(e.Row.DataItem, "SupplyHour").ToString()) < 4.00)
{
GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.Red;
}
else
{
GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.Black;
}
}
}
}
这样写还是不行啊。为什么???? --------------------编程问答-------------------- GridView1.Rows[i].Cells[5].Attributes.Add("style","color:red");
另外,你要学会调试。打印出 DataBinder.Eva(e.Row.DataItem, "SupplyHour").ToString() 看是否真的满足了条件 --------------------编程问答-------------------- 学习 呵呵呵呵呵
补充:.NET技术 , ASP.NET