Click here to Skip to main content
16,011,170 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to implement nested gridviews programatically. in which both gridviews are interlinked with each other(e.g. Heads and Sub-heads tables). I am using entity framework. Now problem in this code is that no proper subheads are appearing according to corresponding heads.

XML
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>


    </div>
    </form>
</body>



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

namespace nested_gridview
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hitechLatestEntities database = new hitechLatestEntities();
              
            GridView1.DataSource = database.HEADs;
       
            TemplateField tfObject = new TemplateField();
            tfObject.HeaderText = "Sub-Heads";
            tfObject.HeaderStyle.Width = Unit.Percentage(50);

            tfObject.ItemTemplate = new myTemplate(ListItemType.Item);
            
            GridView1.Columns.Add(tfObject);
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                hitechLatestEntities data = new hitechLatestEntities();
                var result = from t in data.SUB_HEAD
                             join x in data.HEADs on t.head_code equals x.head_code
                             select t;
                string headCode = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                GridView sub = e.Row.FindControl("newgrid") as GridView;
                sub.DataSource = result;
                sub.DataBind();
            }
        }
    }

    //////////////////////// Class for template field //////////////////////////
    public class myTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {            
            if (myListItemType == ListItemType.Item)
            {
                hitechLatestEntities data = new hitechLatestEntities();
                GridView newgrid = new GridView();
                
                newgrid.DataSource = data.SUB_HEAD;                
                container.Controls.Add(newgrid);
            }
        }
        
        private ListItemType myListItemType;

        public myTemplate()
        {
        }
   
        public myTemplate(ListItemType Item)
        {
            myListItemType = Item;
        }              
    }
    //////////////////////////////////////////////////////////////////////////////////
}
Posted
Updated 28-Jan-14 10:53am
v4
Comments
JoCodes 28-Jan-14 23:06pm    
SubHeads not appearing means? Is it the Data is fetched for the SubHead in the RowDataBound event but not displayed in the child Grid?
Basit Elahi 29-Jan-14 4:09am    
yes data is being fetched by RowDataBound but problem is that same child grid is showing up in each row. i want child gridview according to parent gridview key.

1 solution

It seems to be the Linq query in the RowDataBound Event.

Use a Where condition to filter with the HeadCode before Select statement.

Something like

SQL
var result = from t in data.SUB_HEAD
             join x in data.HEADs
         on t.head_code equals x.head_code
             Where t.head_code=="HeadCodeSelectedFromGrdiviewDataKey"                        select t;
 
Share this answer
 
v2
Comments
Basit Elahi 29-Jan-14 4:07am    
i did like this

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string headCode = GridView1.DataKeys[e.Row.RowIndex].Value.ToString(); /* it is giving error on this line as "Index was out of range. Must be non-negative and less than the size of the collection" */

hitechLatestEntities data = new hitechLatestEntities();
var result = from t in data.SUB_HEAD
join x in data.HEADs on t.head_code equals x.head_code
where t.head_code == "headCode"
select t;

GridView sub = e.Row.FindControl("newgrid") as GridView;
sub.DataSource = result; /* also on this line as "Object reference not set to an instance of an object." */
sub.DataBind();
}
}

when i put try catch around it, it shows the same result as previous. but child grid is showing up in every row whether it is of that head or not.
JoCodes 29-Jan-14 4:11am    
Make sure you have set the DataKeys property in the Gridview1 Markup . Because of that it gives index out of range exception
JoCodes 29-Jan-14 4:13am    
Object reference error is due to the newgrid was not able to findcontrol . check it.
Basit Elahi 29-Jan-14 5:27am    
Thanks a lot @JoCodes. Problem solved..!!!
JoCodes 29-Jan-14 6:53am    
Welcome Basit, Glad to hear that. May be you can upvote it if you feel so since already got an downvote for it. :)

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