Fred
Fred

Reputation: 378

How can I hide columns in GridView?

I have a GridView, as illustrated below, in which I want to display the results from a Stored Procedure in SQL Server. Depenting on inputs of its arguments, The stored procedure returns different results with different number of columns. However its first two columns are always measId and valSeq, which are considered as DataKeyNames in the GridView. I want to display all the available columns in the stored procedure, but measId and valSeq. Instead I want to display 2 TemplateField.

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
     DataSourceID="SqlDataSource1" Width="100%"  BackColor="#DEBA84" 
     BorderColor="#DEBA84" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
     CellSpacing="2" DataKeyNames="measId,valSeq" 
     onrowupdating="GridView_RowUpdating" Visible="False" 
     EmptyDataText="No data!" EnableModelValidation="True" 
     onrowdatabound="GridView1_RowDataBound">
     <Columns>
        <asp:TemplateField>
        <HeaderTemplate>
            <asp:CheckBox ID="cbCheckAll" runat="server" OnClick="javascript:selectAll(this)" />
        </HeaderTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="Checkbox1" runat="server" />
        </ItemTemplate>
        <ItemStyle Width="25px" />
        </asp:TemplateField>
        <asp:CommandField HeaderText="Action" ShowEditButton="True" />
     </Columns>
     <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
     <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" 
         Font-Names="Verdana" Font-Size="Small" HorizontalAlign="Left" />
     <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
     <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" Font-Names="Verdana" 
         Font-Size="X-Small" HorizontalAlign="Left" />
     <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:GridView>

I could successfully declare a SqlDataSource to retrieve the results from the stored procedure and bound it with the GridView. The GridView displays the TemplateFileds as well as all the retrieved columns from the stored procedure. Now I want to hide measId and valSeq columns. But I couldn't find any way to do that. It seems that I cannot have access to the fields which are automatically bound to the GridView.

Upvotes: 3

Views: 8443

Answers (3)

Fred
Fred

Reputation: 378

I finally found my answer here. However since I wanted to hide the 3rd column, I had to add a conditional statement. Hence if you want to hide the 3 column, the solution would be the following:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.Cells.Count > 2)
            e.Row.Cells[2].Visible = false;
    }

Upvotes: 4

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3192

to set a column invisible using the GridView's RowDataBound event.

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[index].Visible = false;
}

0r

this.myGridview.Columns[0].Visible = false;

Here 0 is the column index

Hiding column in GridView

protected void GridView_PreRender(object sender, EventArgs e)
{
    foreach (DataControlField column in GridView1.Columns)
        if (column.HeaderText == "XXXXXXX")
            column.Visible = false;
}

Upvotes: 3

Christopher Peterson
Christopher Peterson

Reputation: 93

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

        <Columns>
            <asp:BoundField HeaderStyle-CssClass="hidden" DataField="TemplateID" HeaderText="Template ID" 
                ReadOnly="True" SortExpression="TemplateID" >
                   <ItemStyle CssClass="hidden"/>
            </asp:BoundField>
            <asp:BoundField DataField="TemplateName" HeaderText="Template Name" 
                SortExpression="TemplateName" />
        </Columns>

Upvotes: 3

Related Questions