zoukankan      html  css  js  c++  java
  • 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)
     

  • 相关阅读:
    对ManualResetEvent和AutoResetEvent的巩固练习
    经纬度点距离的那点儿事
    【读书笔记】C++Primer---第三章
    .NET应用程序调试—原理、工具、方法
    【读书笔记】C++Primer---第二章
    【读书笔记】C++Primer---第一章
    8 个最好的 jQuery 树形 Tree 插件
    C++中引用(&)的用法和应用实例
    自娱自乐之直接插入排序
    自娱自乐之堆排序
  • 原文地址:https://www.cnblogs.com/stswordman/p/449039.html
Copyright © 2011-2022 走看看