All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET2.0中GridView控件的隐藏列的问题_.net资料_编程技术

Writer: aaa Article type: Programming skills(编程技巧) Time: 2013/12/18 7:54:04 Browse times: 337 Comment times: 0

ASP.NET2.0中GridView控件的隐藏列的问题_.net资料_编程技术


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET2.0中GridView控件的隐藏列的问题_.net资料_编程技术-你的首页-uuhomepage.com

  Asp.net2.0GridView隐藏列visible="false" 后你就无法取得这列的值了, 而用datagrid就没有这个问题, MS这个混蛋老是改变游戏规则, 幸好我聪明, 在百度上搜到了别人的解决方法, 然后加入了自己的方法, 才解决问题:

  protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  //隐藏不必要的列
  if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer))
  {
  e.Row.Cells[0].Visible = false;
  e.Row.Cells[3].Visible = false;
  }
  }

  这是迄今为止最简洁的解决方法了。

  解决方案

  在RowCreated事件中书写如下代码:

  void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow ||
  e.Row.RowType == DataControlRowType.Header)
  {
  e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
  }
  //可以根据需要设置更多的列
  }

  因为在RowCreated事件(隐藏)在绑定时候发生,所以这样就即能将数据绑定到列上,又隐藏了该列.所以可以访问到隐藏列的值。

  下面介绍另外一个可以将数据绑定到GridView控件的方法:

Public void myTestFunction()
  {
  string conString="....";//省略
  string sqlquery="...";//省略
  SqlConnection con = new SqlConnection(conString);
  SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
  DataSet ds = new DataSet();
  da.Fill(ds);
  ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
  GridView1.DataSouce = ds.Tables[0];
  GridView1.DataBind() ;
  }

  文章主要讲述了ASP.NET2.0中GridView控件的隐藏列的问题.






There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.