locked
Gridview Hide Columns RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have dropdown & gridview . On the basis of Dropdown selection i want to hide/Unhide columns in Gridview

    Thanks

    Saturday, October 6, 2018 4:27 PM

All replies

  • User-1716253493 posted

    gv1.Columns[1].Visible=false;

    Sunday, October 7, 2018 12:43 AM
  • User-1171043462 posted
    Refer

    https://www.aspforums.net/Threads/181853/How-to-hide-GridView-columns-from-code-behind-using-C-in-ASPNet/

    https://www.aspsnippets.com/Articles/Dynamically-Show-Hide-TemplateField-column-in-ASPNet-GridView-using-C-and-VBNet.aspx
    Sunday, October 7, 2018 7:55 AM
  • User-893317190 posted

    Hi JagjitSingh,

    You could also use jquery to help you.

    Below is my code.

    <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>1show</asp:ListItem>
                <asp:ListItem>1hide</asp:ListItem>
                <asp:ListItem>2show</asp:ListItem>
                <asp:ListItem>2hide</asp:ListItem>
    
    
            </asp:DropDownList>
    
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"  >
                <Columns>
                    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                    <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                    <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                    <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
                    <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                    <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
                    <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
                </Columns>
            </asp:GridView>
           
           <script>
            $(function(){
    
                $("#DropDownList1").change(
                    function () {
                        $("#GridView1 tbody").children().each(function () {
                            if ($("#DropDownList1").val() == "1hide") {
                            $(this).children().eq(0).hide();
                            }
                            if ($("#DropDownList1").val() == "1show") {
                                $(this).children().eq(0).show();
                            }
                             if ($("#DropDownList1").val() == "2hide") {
                            $(this).children().eq(1).hide();
                            }
                            if ($("#DropDownList1").val() == "2show") {
                                $(this).children().eq(1).show();
                            }
                            
                        })
                        
                    }
                )
    
    })
           </script>

    And the result.

    Best regards,

    Ackerly Xu

    Monday, October 8, 2018 5:07 AM