Nested Gridview to show Master/Details relationship in ASP.NET 2.0

July 26, 2006 at 5:02 am 32 comments

This will help you to nest grdiviews inside ASP.NET. There may be many ways to do this, but this is my way( May not be the best, let me know if you have any other 🙂 ).

1. First drop a gridview in to the page, also set its DataKeyNames to your primary key

2. Add a new template column to this gridview.

3. Place another gridview inside this template column.

the code for the termplate column may look like this

<asp:TemplateField HeaderText=”Heading”>
<EditItemTemplate>
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:GridView ID=”GridView1″ runat=”server” DataSource='<%# GetTrasnl(Convert.ToInt32(Eval(“pId”))) %>’ AutoGenerateColumns=”False” CellPadding=”4″ ForeColor=”Black” GridLines=”Vertical” BackColor=”White” BorderColor=”#DEDFDE” BorderStyle=”None” BorderWidth=”1px”>
<FooterStyle BackColor=”#CCCC99″ />
<Columns>
<asp:BoundField DataField=”language” HeaderText=”Language” />
<asp:BoundField DataField=”uName” HeaderText=”Translator” />
</Columns>
<RowStyle BackColor=”#F7F7DE” />
<SelectedRowStyle BackColor=”#CE5D5A” Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#F7F7DE” ForeColor=”Black” HorizontalAlign=”Right” />
<HeaderStyle BackColor=”#6B696B” Font-Bold=”True” ForeColor=”White” />
<AlternatingRowStyle BackColor=”White” />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>

This child gridview has to show the content based on the primary of the row thats binded to main gridview.

Here in my case “pId” is the primary key. The nesting happens here

<asp:GridView ID=”GridView1″ runat=”server” DataSource='<%#GetTrasnl(Convert.ToInt32(Eval(“pId”))) %>’

what I have done is that, I have called function “GetTrasnl” and passed the primary key to that function. This function will return a datatable. So each time a row is bound to the main gridview, the child gridview is also bound with the corresponding values.

The functions is like this

Public Function GetTrasnl(ByVal prgId As Integer) As DataTable

………………
……………….

End Function

Please post your comments if you have any

Advertisements

Entry filed under: ASP.NET.

Page post back after JavaScript code executed on server control click Error : A potentially dangerous Request.Form value was detected from the client

32 Comments Add your own

  • 1. Youkers  |  August 3, 2006 at 5:54 pm

    Thanks,

    Should have been able to figure this out but it helped to see an example.

    Reply
  • 2. Test  |  September 7, 2006 at 12:52 pm

    Worst

    Reply
    • 3. mamta  |  May 16, 2012 at 9:05 am

      nice

      Reply
  • 4. Apply theme programmatically in asp.net 2.0  |  October 3, 2006 at 8:54 am

    thanks for sharing 😉 I was using some example from microsoft that is a little more complicated http://msdn2.microsoft.com/en-us/library/ms404832.aspx

    Reply
  • 5. Kamal  |  November 15, 2006 at 9:47 pm

    Hi,
    First of all I would like thank u and tell u that it is actually a great,Quick and easiest way to bind a child grid to Master row data (DataKeyNames or ID).
    Now After Binding nested grids (More than two child grids), I dont know how to get values or status of Template feild’ checkBoxes in the innermost grids at one button click or event.

    Please do not Ignore this Query and reply me as soon as possible.

    Thanks in Advance!

    Reply
  • 6. Jerry  |  November 29, 2006 at 10:08 am

    Hi,
    I was so surprised to solve my problem this easy.
    Thanks!!!

    Reply
  • 7. Imran  |  January 4, 2007 at 6:31 am

    Easiest way to create nested gridview.

    http://msdn2.microsoft.com/en-us/library/aa992038(vs.80).aspx

    Reply
  • 8. Sharad  |  August 20, 2007 at 3:58 pm

    public and private function was the key to my solution. Thanks!!

    Reply
  • 9. SRINIVAS  |  August 23, 2007 at 9:33 am

    It s good.Helped me out a bit.If you want a better solution search in Quickstarts

    Reply
  • 10. Unsetry  |  November 1, 2007 at 12:20 pm

    ONLINE – DRUGSTORE!
    PRICES of ALL MEDICINES!

    FIND THAT NECESSARY…
    VIAGRA, CIALIS, PHENTERMINE, SOMA… and other pills!

    Welcome please: pills-prices.blogspot.com

    NEW INFORMATION ABOUT PAYDAY LOANS!

    Welcome please: payday-d-loans.blogspot.com

    GOOD LUCK!

    Reply
  • 11. akrouj  |  January 17, 2008 at 4:44 pm

    Very easy and clean code , it worked perfectly for me .

    Thank you very much

    Reply
  • 12. sfds  |  February 8, 2008 at 6:26 am

    arrer
    wr

    Reply
  • 13. sfds  |  February 8, 2008 at 6:27 am

    yahoo.com

    Reply
  • 14. Wilson  |  February 21, 2008 at 5:22 pm

    Hi
    Very good!! Thanks for share

    Reply
  • 15. abc  |  April 16, 2008 at 10:50 am

    abc, testing

    Reply
  • […] Nested Gridview to show Master/Details relationship in ASP.NET 2.0 « Programming Language Tutorials […]

    Reply
  • 17. yellow gold engagement rings  |  June 26, 2008 at 8:35 am

    When shopping for a platinum and diamond ring you don’t always want to go for the largest stone. There are other factors to consider including clarity, cut and color.

    Reply
  • 18. Rob Collins  |  July 29, 2008 at 11:52 pm

    Totally awesome. This saved me so much work over the steps I had seen (and used) elsewhere.

    Thank you so much for sharing your code.

    Best Regards,

    Rob Collins

    Reply
  • 19. Bujji  |  October 29, 2008 at 10:14 am

    Thank u for so much ur code

    Reply
  • 20. Navneet  |  November 7, 2008 at 8:14 am

    This is really superb…gud 1.thanks i feel so my problem is solved

    Reply
  • 21. coder  |  January 8, 2009 at 11:33 am

    this is goood.thnkx. to more
    http://www.codexamples.blogspot.com/

    Reply
  • 22. RaiulBaztepo  |  March 29, 2009 at 1:04 am

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language 😉
    See you!
    Your, Raiul Baztepo

    Reply
  • 23. Ilyas  |  July 28, 2009 at 3:10 pm

    Thank you for a wonderfull Post.

    Reply
  • 24. PrashanthSparks  |  July 29, 2010 at 9:02 pm

    I am also looking for best way to implement my complex GUI.
    It has multiple rows & multiple dataviews inside… user can have option to add/delete/update

    Ex : Request ABC
    may or maynot have child products 1
    may or maynot have child products 2
    may or maynot have child products 3
    may or maynot have child producst 3

    finally it is a one request to submit.

    guess its too much complex, i need to break this GUI.. any help on this..

    Reply
  • 25. melony  |  September 29, 2010 at 4:36 am

    AzafBE http://cgE8hcmk9Vvqlosr5wcBa6nk.com

    Reply
  • 26. Anish Kumar  |  June 3, 2011 at 10:03 am

    Hi all, i am try to use the method for nesting my grid but i am getting the message that say

    “CS0103: The name ‘FillRoleGroupPermissionInChildGrid’ does not exist in the current context”

    How to solve this issue.

    Reply
  • 27. Wtzumdoh  |  September 3, 2011 at 7:47 pm

    I’m doing a masters in law xxx young japan ejjg

    Reply
  • 28. Pdgoobsb  |  September 4, 2011 at 2:12 pm

    I’d like to open an account kiddy sex comics azmb

    Reply
  • 29. Bdxlqbwe  |  September 6, 2011 at 8:33 pm

    History lolita portal
    😛

    Reply
  • 30. Thtuuwqc  |  September 7, 2011 at 10:11 pm

    I’m not sure bb guns
    803

    Reply
  • 31. Pnicqvzm  |  September 8, 2011 at 8:41 pm

    What are the hours of work? young nn
    083

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Top Clicks

  • None

Calendar

July 2006
M T W T F S S
    Aug »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Recent Posts

Feeds

Blog Stats

  • 92,980 hits

%d bloggers like this: