随笔 - 229  文章 - 2  评论 - 511  阅读 - 84万

GridView 隐藏列问题

说明

    GridView 是ASP.NET 2.0的新增控件之一,它的出现代替了原有的DataGrid控件.如果你使用过ASP.NET 2.0. 在设计GridView控件时你拖拽了一个Bound Field,那你可能会遇到一个问题.在早期的.NET版本中,如果想要访问一列,但令它不可见,你可以将他的Visible属性设置为false.
     但是这在ASP.NET 2.0时无效的.当一个列的可见性设置为false,控件不会再将数据绑定到该列中,所以你尝试得到隐藏列的值时,只能得到一个空的字符串.
However, this does not work in ASP.Net 2.0. When a column's visibility is set to False, then the Grid does not bind data to the column, and thus when you try to retrieve data from the hidden column, it either blows up or returns an empty string.

   对于开发人员来说这个正是可大麻烦,现在提供一个解决方法,希望对你您会有帮助

条件

   必须拥有Visual Studio 2005 或者 Visual Web Developer Express并对ASP.NET 2.0有一定了解

解决方案

在RowCreated事件中书写如下代码

  void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = true;  //如果想使第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控件的隐藏列的问题..希望你能构看到本文.希望得到您的反馈,不论是赞同还是否定的.联系方式 msalmank@gmail.com.


原文:Hidden Column Problem (And Two Common Solutions)

  读完老外的文章后,我测试了DetailView控件,也发现了类似的问题,
不过是隐藏行的问题,不过仍然可以访问到隐藏行的的一列(HeaderText)
 

posted on   stswordman  阅读(6752)  评论(10编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2006年7月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示