Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi...I have nested grid....I want to show data when clicking plus image in grid view...
I am getting all details..But the problem is whenever I'm clicking the plus image in parent grid It's showing all the data's in the table detail...

But I want to show only data related to that particular id when I'm clicking that image...

can any one help me?

For Instance

Table_Master have id,Primary member,gender, ....etc
Table_details have id,dependent member,gender,age.....etc

Now I want to write query to show in child grid...

when I'm clicking '+'..I want to the details of the dependent member....
ID in both table is common...That is primary key id....

What I have tried:

select a.name,a.age,a.Gender, r.Relationname as Relation from table_master b 
    inner join table_detail a  on  a.Enrollautoid=b.Enrollautoid  and a.id=b.id
    left join table_relation r on a.Relationautoid=r.Relationautoid 
    where MARK=0 or MARK is null and a.id=b.id and a.Enrollautoid=b.Enrollautoid                
Posted
Updated 2-Mar-17 5:27am
v3
Comments
Garth J Lancaster 28-Feb-17 0:04am    
how is the grid populated and what with ? surely one of the items from the selected row when you click on the '+' gives you the 'key' to constrain your SQL with - please update your question to make it clearer to us what you need - at the moment 'grid' and your SQL statement seem 'disconnected'
Developer29 28-Feb-17 0:13am    
I'm getting output with this query...But it is displaying all the details...I want only that whoever is dependent on that primary...
Developer29 28-Feb-17 0:09am    
In the parent gird...I'm displaying few details for instance id,name of the primary person, and when I'm clicking the plus I have to display the dependent member of that primary person....I have few common columns in two table like id,Enrollautoid..
Garth J Lancaster 28-Feb-17 0:53am    
surely if you have a 'master' record selected, and you click the plus symbol, you then select from 'details' WHERE ID = ID-From-Grid

Where the ID-From-Grid is the "master' ID

ie

SELECT (whatever fields/columns)
FROM Table_Detail D
WHERE D.Id = ID-From-Grid;

I dont see why in your select statements as shown you're going off to select all the master vs details records

Developer29 28-Feb-17 0:58am    
how to get id from grid....

1 solution

Hope this will help you.I Have included the ASP.NET code for you.

public class Master
{
	public Master()
	{
		
	}

    public int Id { get; set; }

    public string PrimaryMember { get; set; }

    public string Gender { get; set; }
}


public class Details
{
	public Details()
	{
	
	}

    public int Id { get; set; }

    public string DependentMember { get; set; }

    public string Gender { get; set; }

    public int MasterId { get; set; }
}


public static class GridRepository
{


    public static List<Master> GetMasters()
    {
        var masters = new List<Master>
        {
            new Master{Id=1,PrimaryMember="VijaiAnand",Gender="Male"},
            new Master{Id=2,PrimaryMember="BalaSundar",Gender="Male"}
        };
        return masters;
    }

    public static List<Details> GetDetails()
    {
        var Details = new List<Details>
        {
            new Details{Id=1,DependentMember="cera",Gender="Female",MasterId=1},
            new Details{Id=2,DependentMember="John",Gender="Male",MasterId=1},
            new Details {Id=3,DependentMember="Srini",Gender="Male",MasterId=2}
        };
        return Details;
    }
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound" DataKeyNames="Id">
            <Columns>
                <asp:TemplateField HeaderText="DetailsGrid">
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >
                            <Columns>
                                <asp:BoundField DataField="DependentMember" HeaderText="DependentMember" />
                                <asp:BoundField DataField="Gender" HeaderText="Gender" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="PrimaryMember" HeaderText="PrimaryMember" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" />
            </Columns>
        </asp:gridview>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindMasterGrid();
        }
    }

    protected void BindMasterGrid()
    {
        var masters = GridRepository.GetMasters();
        Gridview1.DataSource = masters;
        Gridview1.DataBind();
    }

    
    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int masterId = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            masterId = Convert.ToInt32(Gridview1.DataKeys[e.Row.RowIndex].Value);
            GridView grdDetails = e.Row.FindControl("GridView2") as GridView;
        
            if (grdDetails != null)
            {
                grdDetails.DataSource = GridRepository.GetDetails().Where(id => id.MasterId == masterId);
                grdDetails.DataBind();

            }
        }
    }
}


You have to Include the logic for + image control inside that Gridview1 and toggle that via JavaScript.
 
Share this answer
 
v2

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