Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi. I am trying to make a nested gridview with plus and minus button. When I click on the button, the gridview does not go to its proper place. Can someone please check if I lack something here.

Here is the javascript that also referes to a jquery script
HTML
<link rel="shortcut icon" href="http://server15/DefaultDesign/favicon.png" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function pageLoad() {
            $(function () {
                $("[src*=minus-level1]").each(function () {
                    $(this).closest("tr").after("<tr><td style='border:1px solid #CCC;'></td><td style='border:1px solid #CCC;' colspan = '999999'>" + $(this).next().html() + "</td></tr>");
                    $(this).next().remove()
                });
           
        }


Here is my gridview design

ASP.NET
<asp:GridView id="gvTransactionsMain" runat="server" CellPadding="3" AutoGenerateColumns="false" OnRowDataBound="gvTransactions_RowDataBound" Width="1006px"  EmptyDataText="No Transaction found in the database.">
<Columns>
    <asp:TemplateField ItemStyle-Width="16px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
    <ItemTemplate>
         <asp:ImageButton ID="imgLevel1" runat="server" CommandArgument="Show" ImageUrl="~/Images/GridViewButtons/plus-level1.png" Width="16px" Height="16px" OnClick="imgLevel1_Click" />
         <asp:HiddenField ID="hdnTrxNum" runat="server" Value='<%# DataBinder.Eval(Container.DataItem,"TrxNum") %>' />
         <asp:Panel ID="pnlLevel1" runat="server" Style="position:relative;" Visible="false">
              <div  style="padding:5px">
              <asp:GridView ID="gvTransactionsDet" runat="server" CellPadding="3" AutoGenerateColumns="false" EmptyDataText="No Transaction found in the database.">
              <Columns>
                     <asp:TemplateField HeaderText="Trx Date" ItemStyle-Width="70px" ItemStyle-HorizontalAlign="Center">
                     <ItemTemplate>
                           <asp:Label ID="lblDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"TrxDate","{0:MM/dd/yyyy}") %>'></asp:Label>
                           <asp:HiddenField ID="hdnTransNumber" runat="server" Value='<%# Eval("TrxNum") %>' />
                      </ItemTemplate>
                      </asp:TemplateField>
              </Columns>
              </asp:GridView>
</Column>
</asp:GridView>


Here is the imgLevel1_Click event that handles the plus button

C#
protected void imgLevel1_Click(object sender, EventArgs e)
    {
        ImageButton imgShowHide = sender as ImageButton;
        GridViewRow row = imgShowHide.NamingContainer as GridViewRow;

        if (imgShowHide.CommandArgument == "Show")
        {
            imgShowHide.CommandArgument = "Hide";
            imgShowHide.ImageUrl = "~/Images/GridViewButtons/minus-level1.png";
            row.FindControl("pnlLevel1").Visible = true;
        }
        else
        {
            row.FindControl("pnlLevel1").Visible = false;
            imgShowHide.ImageUrl = "~/Images/GridViewButtons/plus-level1.png";
            imgShowHide.CommandArgument = "Show";
        }
    }


What I have tried:

I tried the above codes and it the 2nd level gridview appears on the first column with the plus/minus button.
Posted
Updated 28-Apr-16 22:57pm
Comments
Richard Deeming 27-Apr-16 8:26am    
What does "the gridview does not go to its proper place" mean?

Remember, we can't see your screen, access your computer, or read your mind. When you describe the problem, you have to assume that we know nothing about your code, your requirements, or the problem you're trying to solve.
bjay tiamsic 28-Apr-16 19:21pm    
Sorry. Here it is. The first link is the my current outcome.
https://www.dropbox.com/s/eq19zfuq6iystov/Capture.JPG?dl=0

And this one is the desired interface.

https://www.dropbox.com/s/ujqaufu7gn8hz2u/CorrectDesign.JPG?dl=0

1 solution

Try assigning a class for the child grid inside Panel (ie for grid gvTransactionsDet).

Something like:
JavaScript
<asp:GridView ID="gvTransactionsDet" runat="server" CellPadding="3" CssClass = "ChildGrid" AutoGenerateColumns="false" EmptyDataText="No Transaction found in the database.">
 
Share this answer
 
Comments
bjay tiamsic 1-May-16 19:37pm    
So it is a CssClass? What style should I consider?
iamcpmember 3-May-16 6:39am    
Yes. It is a CssClass. The style information should be already defined inside this CssClass. The name of the style may vary according to your source.

You may refer to the following links:
1. http://www.aspsnippets.com/Articles/ASPNet-Nested-GridViews-GridView-inside-GridView-with-Expand-and-Collapse-feature.aspx

2. https://vijaymeghwal.wordpress.com/2012/08/29/nested-gridview-example-in-asp-net-c/

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