-->

How to Display Master/Detail Data from a Database using ListView and GridView Control in ASP.NET.

Introduction

In this post, I explain how to Display Master/Detail Data from a Database using ListView and GridView Control in ASP.NET.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create 2 table for Master Details Record.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used table as below
Table 1

Table 2

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for show Master Details Record using ListView & Gridview Control in Webpage.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

DXR.png image is this : 


CSS Code
  1. <style type="text/css">
  2. .collapse
  3. {
  4. background-position: left -172px;
  5. height: 14px;
  6. width: 13px;
  7. background-repeat: no-repeat;
  8. background-image: url('DXR.png');
  9. cursor:pointer;
  10. }
  11. .expand
  12. {
  13. background-position: -14px -187px;
  14. height: 14px;
  15. width: 13px;
  16. background-repeat: no-repeat;
  17. background-image: url('DXR.png');
  18. cursor:pointer;
  19. }
  20. table
  21. {
  22. border:solid 1px black;
  23. }
  24. table td
  25. {
  26. border-right:solid 1px black;
  27. border-bottom:solid 1px black;
  28. }
  29. table th
  30. {
  31. border-bottom:solid 1px black;
  32. }
  33. .SUBDIV table {
  34. border:0px;
  35. border-left:1px solid black;
  36. }
  37. </style>
JS Code
  1. <script src="Scripts/jquery-1.7.1.js"></script>
  2. <script language="javascript">
  3. $(document).ready(function () {
  4. // THIS IS FOR HIDE ALL DETAILS ROW
  5. $(".SUBDIV table tr:not(:first-child)").not("tr tr").hide();
  6. $(".SUBDIV .btncolexp").click(function () {
  7. $(this).closest('tr').next('tr').toggle();
  8. //this is for change img of btncolexp button
  9. if ($(this).attr('class').toString() == "btncolexp collapse") {
  10. $(this).addClass('expand');
  11. $(this).removeClass('collapse');
  12. }
  13. else {
  14. $(this).removeClass('expand');
  15. $(this).addClass('collapse');
  16. }
  17. });
  18. });
  19. </script>
HTML Code
  1. <h3>Master Details Record using ListView & Gridview Control in ASP.NET</h3>
  2. <div>
  3. <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
  4. <LayoutTemplate>
  5. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  6. <tr>
  7. <th width="15px"></th>
  8. <th width="15%">Customer Code</th>
  9. <th width="25%">Customer Name</th>
  10. <th width="20%">Contact No</th>
  11. <th width="20%">State</th>
  12. <th>City</th>
  13. </tr>
  14. </table>
  15. <div runat="server" id="itemPlaceHolder"></div>
  16. </LayoutTemplate>
  17. <ItemTemplate>
  18. <div class="SUBDIV" runat="server">
  19. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  20. <tr>
  21. <td width="15px">
  22. <div class="btncolexp collapse">
  23. &nbsp;
  24. </div>
  25. </td>
  26. <td width="15%"><%#Eval("CustomerCode") %></td>
  27. <td width="25%"><%#Eval("CustomerName") %></td>
  28. <td width="20%"><%#Eval("ContactNo") %></td>
  29. <td width="20%"><%#Eval("State") %></td>
  30. <td><%#Eval("City") %></td>
  31. </tr>
  32. <tr>
  33. <td colspan="6">
  34. <div style="margin:20px">
  35. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  36. <Columns>
  37. <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
  38. <asp:BoundField HeaderText="Order No" DataField="OrderNo" />
  39. <asp:BoundField HeaderText="Order Date" DataField="OrderDate" />
  40. <asp:BoundField HeaderText="Quantity" DataField="Quantity" />
  41. <asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" />
  42. <asp:BoundField HeaderText="Total" DataField="TotalAmount" />
  43. </Columns>
  44. </asp:GridView>
  45. </div>
  46. </td>
  47. </tr>
  48. </table>
  49. </div>
  50. </ItemTemplate>
  51. </asp:ListView>
  52. </div>

Step-6: Write code in page_load event for fetch Master data from database and bind to ListView Control for show master details record in webpage.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateData();
  6. }
  7. }
and here is the function
  1. private void PopulateData()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. var v = dc.Customers.ToList();
  6. ListView1.DataSource = v;
  7. ListView1.DataBind();
  8. }
  9. }

Step-7: Write code in ListView1_ItemDataBound event for fetch corresponding details record and bind it to the Gridview(inside ListView).


  1. protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
  2. {
  3. if (e.Item.ItemType == ListViewItemType.DataItem)
  4. {
  5. ListViewDataItem lvItem = (ListViewDataItem)e.Item;
  6. Customer cus = (Customer)lvItem.DataItem;
  7. if (cus != null)
  8. {
  9. GridView gv1 = (GridView)e.Item.FindControl("GridView1");
  10. if (gv1 != null)
  11. {
  12. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  13. {
  14. var v = dc.OrderMasters.Where(a => a.CID.Equals(cus.CID)).ToList();
  15. gv1.DataSource = v;
  16. gv1.DataBind();
  17. }
  18. }
  19. }
  20. }
  21. }

Step-8: Run Application.


Related Post:

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: