How to show data from parent and child table in Gridview or datalist?

Last post 08-23-2008 3:10 AM by ramireddyindia. 1 replies.

Sort Posts:

  • How to show data from parent and child table in Gridview or datalist?

    08-22-2008, 5:37 PM
    • Member
      29 point Member
    • azharrahi
    • Member since 06-17-2006, 6:39 AM
    • Posts 166

    I am working in ASP.Net 2.0.

    I have three tables in Database which are inter-linked by foriegn keys. 

    1. Portfolio (PortfoilioID,PortfolioTitle,PortfolioDescription)

    2. SubPortfolio (SubPortfolioID, SubPortfolioTitle, SubPortfolioDescription, PortfolioID)

    3. SubPortfolioDetails (SubPortfolioDetailID, SubPortfolioDetail, SubPortfolioID)

    Now the Primary key of Portfolio is foreign key in SubPortfolio, while  Primary Key of SubPortfolio is foreign key in SubPortfolioDetails.

    Now I want to show data on the form such that in the first row, then show the data of SubPortfoloio related to Portfolio. And make the Subportfolio data a hyperlink. and then show the data of SubPortfolioDetails related to SubPortfolio on next page when click on the SubPortfolio Hyperlink.

    Such that:

    1. Portfolio Title+  Description 

    SubPortfolio Title (Hyperlink)

    2. Portfolio Title+ Description

    SubPortfolio Title (Hyperlink)

    and so on .

    It just like datalist or gridview.

    How can I do it. Any Idea please?
  • Re: How to show data from parent and child table in Gridview or datalist?

    08-23-2008, 3:10 AM
    Answer

    see this total example,  i used 2 tables

    MM_MaterialGroups(MatGroupID int,MarGroup)

    MM_UnderMatGroups(UnderMatGroupID,UnderMatGroup,MarGroupId)

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ParentChildsandDetails.aspx.cs" Inherits="ParentChildsandDetails" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:MultiView ID="mvMat" runat="server"  ActiveViewIndex="0">
            <asp:View ID="View1" runat="server">
        <asp:Repeater ID="GridView1" runat="server"  DataSourceID="SqlDataSource1">
            <ItemTemplate>
                MaterialGroups<asp:Label ID="lblGroup" Text='<%# Bind("MatGroup") %>' runat="server"  />
                <asp:Label ID="lblId"  Visible="false" Text='<%# Bind("MatGroupId") %>' runat="server"  />     
                
           
                <asp:GridView ID="GridView2" runat="server" OnRowCommand="GridView2_RowCommand"  DataSourceID="SqlDataSource2"  AutoGenerateColumns="true">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"  CommandArgument='<%# Bind("UnderMatGroupID") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mak_testConnectionString %>"
            SelectCommand="SELECT [UnderMatGroupId], [UnderMatGroup] FROM [MM_UnderMatGroups] WHERE ([MatGroupId] = @MatGroupId)">
            <SelectParameters>
                <asp:ControlParameter ControlID="lblId" Name="MatGroupId" PropertyName="Text" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
              </ItemTemplate>
        </asp:Repeater>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mak_testConnectionString %>"
            SelectCommand="SELECT [MatGroupId], [MatGroup] FROM [MM_MaterialGroups]"></asp:SqlDataSource>
            </asp:View>
        <asp:View  ID="View2" runat="server">
        <asp:Button id="Button1" runat="server" Text="Back" OnClick="Button1_Click"></asp:Button>
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="UnderMatGroupId"
                DataSourceID="SqlDataSource2">
                <Columns>
                    <asp:BoundField DataField="UnderMatGroupId" HeaderText="UnderMatGroupId" InsertVisible="False"
                        ReadOnly="True" SortExpression="UnderMatGroupId" />
                    <asp:BoundField DataField="UnderMatGroup" HeaderText="UnderMatGroup" SortExpression="UnderMatGroup" />
                    <asp:BoundField DataField="MatGroupId" HeaderText="MatGroupId" SortExpression="MatGroupId" />
                    <asp:BoundField DataField="CreatedBy" HeaderText="CreatedBy" SortExpression="CreatedBy" />
                    <asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" SortExpression="CreatedDate" />
                    <asp:BoundField DataField="UpdatedBy" HeaderText="UpdatedBy" SortExpression="UpdatedBy" />
                    <asp:BoundField DataField="UpdatedDate" HeaderText="UpdatedDate" SortExpression="UpdatedDate" />
                    <asp:BoundField DataField="CompanyId" HeaderText="CompanyId" SortExpression="CompanyId" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mak_testConnectionString %>"
                SelectCommand="SELECT * FROM [MM_UnderMatGroups] WHERE ([UnderMatGroupId] = @UnderMatGroup)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="HiddenField1" Name="UnderMatGroup" PropertyName="Value"
                        Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
             
            <asp:HiddenField ID="HiddenField1" runat="server" />
        
        </asp:View>
    </asp:MultiView>
    
    </asp:Content>
    
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class ParentChildsandDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "View")
            {
                mvMat.ActiveViewIndex = 1;
                HiddenField1.Value = Convert.ToString(e.CommandArgument);
                GridView2.DataBind();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            mvMat.ActiveViewIndex = 0;
        }
    }
    
     
    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
Page 1 of 1 (2 items)