How do I hide columns in a GridView?

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

    <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 can successfully declare a SqlDataSource to get the results from the stored procedure and bind it to the GridView. The GridView displays the TemplateFileds as well as any retrieved columns from the stored procedure. Now I want to hide the measId and valSeq columns. But I couldn't find any way to do this. It seems that I cannot access the fields that are automatically bound to the GridView.

+3


source to share


3 answers


I finally found my answer here . However, since I wanted to hide the third column, I had to add a conditional statement. Hence, if you want to hide column 3, the solution is as follows:



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

      

+4


source


to set the 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 a column in a GridView

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

      

+3


source


<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>

      

+2


source







All Articles