3

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.

Fred
  • 378
  • 1
  • 10
  • 26

3 Answers3

4

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;
    }
Community
  • 1
  • 1
Fred
  • 378
  • 1
  • 10
  • 26
3
<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>
  • 1
    It's not going to work for the columns which are displayed already. It just adds a new hidden column! – Fred Oct 21 '14 at 00:29
3

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;
}
MMM
  • 3,132
  • 3
  • 20
  • 32
  • OK. I used 2 lines of e.Row.Cells[x].Visible = false; where x = 2 and x =3 to hide the column I wanted (the first two columns are Customized TemplateFields). But it doesn't hide the Headers. How can I hide the headers? BTW, myGridview.Columns only addresses the firs two TemplateFields! – Fred Oct 21 '14 at 00:47
  • It's still not working. Please notice that I'm using Auto Generated fields, which are not collected in **GridView1.Columns**. – Fred Oct 23 '14 at 13:14