gridview选择行。

来源:互联网 发布:智能网络电力仪表 编辑:程序博客网 时间:2024/05/23 17:56
  大家都知道GridView有个属性,叫AutoGenerateSelectButton属性,设置它为TRUE时。会在GRIDVIEW最左端出现一列选择按钮。但是作为表格,它这种选择方法实在不符合平常的使用习惯。
  
   经过查询资料,并加以改造。现在可以实现鼠标移到某行上,该行变颜色。离开后恢复。点击后,选中该行,并变为另一种颜色。
  
  1、首先设置AutoGenerateSelectButton属性为TRUE;
  
  2、
  
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   { //判断是否是数据行
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
  
   //鼠标移动到某行上,该行变色
   e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#999999'");
  
   //鼠标移开后,恢复
   e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
  
   //点击后,实现该行的选中
  
   e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
  
   }
   }
  
  3、protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
   {//设置选中行的颜色
   GridView1.SelectedRowStyle.BackColor = Color.FromArgb(8, 222, 222, 0) ;
   }
  
  4、 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
   {
   //将GRIDVIEW的第一列,即选择列隐藏
  
  
   e.Row.Cells[0].Attributes.Add("style", "display:none;");
  
   }
原创粉丝点击