How to get Hidden Column Value in GridView

1. First Way - Use DataKeyNames Property of GridView


(a)
DataKeyNames is used to specify Primary key coulmn(s) of the data source that is bound to the GridView.

When DataKeyNames property of GridView is set then values of specified columns are stored in DataKeys collection that is available at runtime.


 <asp:GridView ID="dtgOutwardSpare" runat="server" AutoGenerateColumns="False"

AutoGenerateSelectButton="True" CellPadding="4" DataKeyNames="AssetID,OutwardTypeID"

OnSelectedIndexChanged="dtgOutwardSpare_SelectedIndexChanged" Width="890px" ConvertEmptyStringToNull="true" HtmlEncode="false" HtmlEncodeFormatString="false">

 <AlternatingRowStyle BackColor="White" />

<Columns>
<asp:BoundField DataField="AssetID" HeaderText="AssetID" Visible="False" HtmlEncode="False"
  HtmlEncodeFormatString="False" />
<asp:BoundField DataField="AssetCode" HeaderText="AssetCode" HtmlEncode="False" HtmlEncodeFormatString="False" />

<asp:BoundField DataField="AssetName" HeaderText="AssetName" HtmlEncode="False" HtmlEncodeFormatString="False" />
</Columns>


</asp:GridView>



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

   int assetid = Convert.ToInt32(dtgOutwardSpare.SelectedDataKey["AssetID"]);

or

 int itemid = Convert.ToInt32(dtgOutwardSpare.DataKeys[i]["AssetID"].ToString());

2. Second Way -Use StyleSheet


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



  <style>   
 .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:BoundField DataField="OldQuantity" HeaderText="OldQuantity" HtmlEncode="False"
 HtmlEncodeFormatString="False" HeaderStyle-CssClass = "hideGridColumn" ItemStyle-CssClass="hideGridColumn"/>


Comments

Popular posts from this blog

How to retain the data annotations or class during modify a table in the database

Interview Questions

AJAX CONTROL