How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)

原文:http://www.codeproject.com/Tips/80964/How-to-get-Hidden-Column-Value-in-GridView
Suppose I want to display Address details in GridView but don't want to display AddressId then I will hide column that displays AddressId. GridView has CheckBox also to select Addresses. Now at runtime on click of button I want to get AddressId of all selected Addresses in that case if I use gvAddress.Rows[index].Cell[1] to get AddressId it will return Nothing.

The reason behind is, if Gridview has any BoundField column for which visible property is set to "false" then that columns is not rendered at runtime in GridView and data of hidden column won’t be available .

But, there are four other ways to get value of hidden column. 

1. First Way - Use DataKeyNames Property of GridView


(a). GridView has property named "DataKeyNames". DataKeyNames is used to specify Primary key coulmn(s) of the data source that is bound to the GridView. More than one primary key fields can be set to DataKeyNames. All primary key fields name must be separated by comma. When DataKeyNames property of GridView is set then values of specified columns are stored in DataKeys collection that is available at runtime.

So, in following example to get AddressId of all selected Addresses set DataKeyNames property of GridView to "AddressID".

<asp:GridView ID="gvAddress" runat="server" AllowPaging="True"
             AllowSorting="True" AutoGenerateColumns="False" DataKeyNames = "AddressID"
             DataSourceID="SqlDataSource1">
             <Columns>
            <asp:TemplateField ShowHeader="false">
                <ItemTemplate>
                       <asp:CheckBox ID = "chkSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
 
            <asp:BoundField DataField="AddressID" HeaderText="AddressID"
                     Visible="False" />
            <asp:BoundField DataField="AddressLine1" HeaderText="Address Line1"
                     SortExpression="AddressLine1" />
            <asp:BoundField DataField="AddressLine2" HeaderText="Address Line2"
                     SortExpression="AddressLine2" />
                              </Columns>
</asp:GridView>

<!-- DataSource that is bound to GridView -->
 
<SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
            SelectCommand="SELECT AddressID, AddressLine1, AddressLine2, City, PostalCode FROM Person.Address">
</asp:SqlDataSource>

(b). On click of button use DataKeys property of GridView to get AddressId of all selected Addressed.

protected void btnSelect_Click(object sender, EventArgs e)
{
    // Create a list to store selected AddressId
    List<int> lstAddressId = new List<int>();
    Control chkSelect = null;
    for (int iRow = 0; iRow &amp;lt; gvAddress.Rows.Count; iRow++)
    {
        //Find CheckBox control in GridView
        chkSelect = gvAddress.Rows[iRow].Cells[0].FindControl("chkSelect&amp");
        if (chkSelect != null)
        {
          //If CheckBox is checked then get AddressId by using DataKeys
          // properties of GridView and add AddressId of selected row in List.
          if (((CheckBox)chkSelect).Checked)
          {
              int iAddressId = (int) gvAddress.DataKeys[iRow].Value;
              lstAddressId.Add(iAddressId);
          }
        }
     }
}

2. Second Way - Use TemplateField column in GridView


(a). Create TemplateField column for hidden column in GridView, add Label control inside ItemTemplate And set visible to false for <asp:templatefield xmlns:asp="#unknown">. So that TemplateField column won't be displayed at runtime. Value of label can be accessed at runtime by using Label's Text property.

<asp:GridView ID="gvAddress" runat="server" AutoGenerateColumns="False" >
  <columns>
      <asp:TemplateField ShowHeader="false">
          <ItemTemplate>
             <asp:CheckBox ID = "chkSelect" runat="server" />
          </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField Visible=false>
         <ItemTemplate>
             <asp:Label id="lblAddressId" runat ="server" text='<%# Eval("AddressID")%>'>
         </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />
      <asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />
  </columns>
<asp:GridView>

(b). Use FindControl method of Row property of GridView to find Label control defined inside TemplateField column and assign it to label object and get value of label by using text property. 

protected void btnSelect_Click(object sender, EventArgs e)
{
         // Create a List of Integer to store selected AddressId
         List<int> lstAddressId = new List<int>();
         Label lblAddressId = null;
         Control chkSelect = null;
         for (int iRow = 0; iRow &lt; gvAddress.Rows.Count; iRow++)
         {
             //Find CheckBox control in GridView for particular Row
            chkSelect = gvAddress.Rows[iRow].Cells[0].FindControl("chkSelect");
            if (chkSelect != null)
            {
                //If CheckBox is checked means Row is selected then  
                //find Label ”lblAddressId” that 
                //is inside TemplateField in GridView and add 
                //AddressId of selected row in List 
                //by using Label’s Text property.
                if (((CheckBox)chkSelect).Checked)
                {
                     lblAddressId =  (Label)GVAddress.Row[iRow].FindControl("lblAddressId");
                     if (lblAddressId != null)
                     {
                         int iAddressId = (int)  lblAddressId.Text;
                         lstAddressId.Add(iAddressId);
                     }
                }
         }
}

3. Third Way - Use StyleSheet


(a). Create a stylesheet for hiding grid column.


<style type=”text/css”>
    .hideGridColumn
    {
        display:none;
    }
 </style>


(b). Set  HeaderStyle-CssClass and  ItemStyle-CssClass property of column to hideGridColumn, that is defined in css, to hide column.


<asp:GridView ID="gvAddress" runat="server" AllowPaging="True"
             AllowSorting="True" AutoGenerateColumns="False"
             DataSourceID="SqlDataSource1">
             <Columns>
                  <asp:TemplateField ShowHeader="false">
                      <ItemTemplate>
                            <asp:CheckBox ID = "chkSelect" runat="server" />
                     </ItemTemplate>
                  </asp:TemplateField>
 
                 <asp:BoundField DataField="AddressID" HeaderText="AddressID" HeaderStyle-CssClass = "hideGridColumn" ItemStyle-CssClass="hideGridColumn"/>
                 <asp:BoundField DataField="AddressLine1" HeaderText="Address Line1"
                     SortExpression="AddressLine1" />
                 <asp:BoundField DataField="AddressLine2" HeaderText="Address Line2"
                     SortExpression="AddressLine2" />
             </Columns>
         </asp:GridView>

(c). Get value of hidden column i.e. "AddressId" by using gvAddress.Rows[iRow].Cells[1].Text property.

protected void btnSelect_Click(object sender, EventArgs e)
{
        // Create a List of Integer to store selected AddressId
        List<int> lstAddressId = new List<int>();
        Control chkSelect = null;
        for (int iRow = 0; iRow &lt; gvAddress.Rows.Count; iRow++)
        {
            //Find CheckBox control in GridView
            chkSelect = gvAddress.Rows[iRow].Cells[0].FindControl("chkSelect");
            if (chkSelect != null)
            {
                //If CheckBox is checked then add AddressId of selected row in List by using
                // gvAddress.Rows[iRow].Cells[1].Text property
                if (((CheckBox)chkSelect).Checked)
                {
                    int iAddressId = (int)gvAddress.Rows[iRow].Cells[1].Text;
                    lstAddressId.Add(iAddressId);
                }
             }
}


4. Fourth Way - Set Visibility of Column after Binding DataSource to GridView


Set column's visible property to false that is required to hide after binding Data Source to Gridview in Codebehind. In that case value of hidden column can be get by using "GVAddress.Rows[iRow].Cell[1].Text" as shown in the example of Third Way.


// Bind GridView to Data Source
GVAddress.DataSource = dtAddress;
GVAddress.DataBind();
//Set visible to false for AddressId column after binding
//Data Source to GridView
GVAddress.Columns[1].Visible = false;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值