Tuesday, January 1, 2013

Asp.net Hidding columns with different Methods

1) Hiding Columns using CSS:


<style type="text/css">
    .hidden
    {
        display: none;
    }
</style>

 
<asp:GridView runat="server" ID="gvHiddenColumnsCSS" AutoGenerateColumns="False"
    Caption="<b>1) using CSS</b>" CellPadding="4" BorderStyle="Solid" ForeColor="#333333"
    GridLines="None" BorderColor="#507CD1">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="ID" ItemStyle-CssClass="hidden" 
                        ControlStyle-CssClass="hidden" HeaderStyle-CssClass="hidden" 
                        FooterStyle-CssClass="hidden" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" />
</asp:GridView>
 
Above gridview displays only Name column where as ID column will be hidden. Still, we will be able to retrieve ID value as
string strID= gvHiddenCSS.Rows[row Index].Cells[0].Text;
 2) Hiding Columns using DataKeys:
We can use Datakeys to add one or more columns which are not displayed in the GridView. Following my
previous example, We can add ID as Datakeys and remove the boundfield (ID column) from the GridView.
<asp:GridView runat="server" ID="gvHiddenDataKeys" AutoGenerateColumns="False" CellPadding="4" BorderStyle="Solid" ForeColor="#333333" GridLines="None" BorderColor="#507CD1" Caption="<b>2) using DataKeys</b>" DataKeyNames="ID"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" /> </Columns> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> </asp:GridView>
 
We can retrieve ID value as


string strID=gvHiddenDataKeys.DataKeys[row index].Value.ToString();


3) Hiding Columns using Visible Property
 
<asp:GridView runat="server" Caption="<b>3) using Visible Property</b>" ID="gvHiddenVisible"
    AutoGenerateColumns="False" CellPadding="4" BorderStyle="Solid" ForeColor="#333333"                                                                                              
    GridLines="None" BorderColor="#507CD1" OnDataBinding="gvHiddenVisible_DataBinding"
    OnDataBound="gvHiddenVisible_DataBound">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" />
</asp:GridView>
protected void gvHiddenVisible_DataBinding(object sender, EventArgs e)
{
    gvHiddenVisible.Columns[0].Visible = true;
}
protected void gvHiddenVisible_DataBound(object sender, EventArgs e)
{
    gvHiddenVisible.Columns[0].Visible = false;
}

 
 




only Name column where as ID column will be hidden. Still, we will be able to retrieve ID value as

string strID= gvHiddenCSS.Rows[row Index].Cells[0].Text;

 

No comments:

Post a Comment