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


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

    方法二:
    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() ;
     

    方法三:

    private void BindGrid()
        {
            String cmdText = "SELECT KMS_AccountInfo.AccountId, KMS_ServerInfo.ServerId, KMS_ServerInfo.ServerCountryCn, KMS_AccountInfo.StockNum,";
            cmdText += " KMS_ServerInfo.ServerName,";
            cmdText += " KMS_ServerInfo.GameTypeName,dbo.getmneed(KMS_ServerInfo.ServerId) AS needNum, (dbo.getmkc(KMS_ServerInfo.ServerId)-KMS_AccountInfo.StockNum) AS kcnum,dbo.getmkc(KMS_ServerInfo.ServerId) - dbo.getmneed(KMS_ServerInfo.ServerId) AS CanConvertNum FROM KMS_AccountInfo INNER JOIN";
            cmdText += " KMS_ServerInfo ON KMS_AccountInfo.ServerId = KMS_ServerInfo.ServerId inner join kms_gameinfo on KMS_ServerInfo.gameid=kms_gameinfo.gameid Where KMS_ServerInfo.ServerName like '" + txt_ServerName.Text.Trim().ToString() + "%' and KMS_AccountInfo.IsRealStock = 0 and kms_gameinfo.gameid=";
            cmdText += Convert.ToInt32(Session["OperatorGame"]);
            cmdText += " ORDER BY KMS_ServerInfo.ServerCountryCn DESC, KMS_ServerInfo.ServerName";
            SqlDataAdapter myadapter = new SqlDataAdapter(cmdText, myConnection);
            DataSet DsOrderList = new DataSet();
            myadapter.Fill(DsOrderList);
            StockList.DataSource = DsOrderList;
            StockList.DataBind();
            StockList.Columns[0].Visible = false;
            if (myConnection.State.ToString() == "Open") myConnection.Close();
        }

    在.aspx文件中设置隐藏列的Visible 属性为true  绑定后设定其属性为false即可

  • 相关阅读:
    C++字符串(srtring)反转
    字典(Dictionary)
    畅通工程
    子串计算
    神奇的口袋
    SLT 优先队列 哈弗曼树最小带权路径
    大数阶乘
    整数拆分
    A+B (带有,的数字)
    Hdu 1232 畅通工程
  • 原文地址:https://www.cnblogs.com/ly5201314/p/1398070.html
Copyright © 2011-2022 走看看