Click here to Skip to main content
15,945,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I am using a GridView inside a Repeater control to generate three GridViews with 3 similar columns and different number of different columns between them. Everything works fine and well. I am just facing one probelm with the filtered results.
I have a Filter as dropdownlist based on the Division name. This filter shows the following values: All and the division names which are retrieved from the database.
Instead of showing the divison name when I filtered the results based on the one of these divisions, I hide the Division column programmatically. Now, I want when the value of filter is All to show the Divison column when it is other than All value to show that column. So how to do that?

My ASP.NET code:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                    SelectCommandType="StoredProcedure" SelectCommand="kbiReport"
                                    FilterExpression="[DivisionName] like '{0}%'">
                        <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 
                                                 PropertyName="SelectedValue" Type="String" />

                        <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values 
                            of GroupID--%>
                        <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />

                <div id="divGrid" class="tableDiv" style=" width:900px">
                <asp:GridView ID="GridView1" runat="server" 
                                ClientIDMode="Static" class="fixedTables" Width="600" AutoGenerateColumns="true"
                                OnRowDataBound="GridView1_RowDataBound" OnPreRender="GridView1_PreRender" OnRowCreated="GridView1_RowCreated">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <HeaderStyle Font-Bold = "true" ForeColor="Black"/> 
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

And the code-behind for hiding the first column is:

//This method is for deleting the first column in the GridView
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        e.Row.Cells[0].Visible = false; // hides the first column
manognya kota 10-Jan-12 8:00am    
Could not get the last lines..
' Now, I want when the value of filter is All to show the Divison column when it is other than All value to show that column.'
In either of the cases, you want to show the column?
matrix388 10-Jan-12 22:59pm    
I just want the Division column to be displayed when the value of the filter is ALL

1 solution


On selected index changeed event of your dropdown list hide the gridview column as follows.

protected void yourdropdownid_SelectedIndexChanged(object sender, EventArgs e)
            if (DLState.SelectedItem.Text == "All")
                GridView1.Columns[0].Visible = true;
                GridView1.Columns[0].Visible = false;

Hope this helps.
Share this answer
matrix388 11-Jan-12 0:54am    
the GridView is nested inside a Repeater, so how I can find it 11-Jan-12 5:08am    
post your aspx page ...
matrix388 14-Jan-12 3:27am    
It is already posted above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900