日期:2014-05-18  浏览次数:20863 次

如何实现GridView1查询出来数据根据查询内容改变颜色
 

用的数据动态绑定
protected void Page_Load(object sender, EventArgs e)
  {
  string sqlstr,water_dah;
  if (Request.QueryString["dah"].ToString().Trim()== null)
  {
  Response.Write("没有查到记录");
  Response.End();

  }
  else
  {
  sqlstr = "select * from record";
  string connectionstring = "保密";
  SqlConnection conn1 = new SqlConnection(connectionstring);
  SqlCommand cmd = new SqlCommand(sqlstr, conn1);
  conn1.Open();
  GridView1.DataSource = cmd.ExecuteReader();
  GridView1.DataBind();
  }
  dd.Close();
  }

  }

//我在record表一个字段 color_no,用来显示查询出来数据行颜色, 
//当color_no=1查询出来该行字体显示为 #FFFFFF 
//当color_no=2查询出来该行字体显示为 #000000 
//当color_no=3查询出来该行字体显示为 #999999 
//改如何使用下面函数 要在GridView1写什么事件,如何调用color_no值????

 public string getColor(string color_no)
  {
  switch (color_no)
  {
  case "1":
  return "#FFFFFF";
  break;
  case "2":
  return "#000000";
  break;
  case "3":
  return "#999999";
  break;

  }
  }


------解决方案--------------------
探讨
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
string text = gvr.Cells[3].Text;
switch (text)
{
case "1":
gvr.BackColor = System.Drawing.Color.Red;
break;
case "2":
gvr.BackColor = System.Drawing.Color.Blue;
break;
}
}
}