adding rows to a child  gridview  in a  nested gridview

Posted on 2007-07-22
Last Modified: 2013-11-26

Iam really frustrated in trying to add new rows to my child grid view. The layout is something like this  there will be categories and items in the database, I have to first display the items under each categories. and each of the categories will have an "addnew" button under the displayed items.  So suppose there are 5 categories and respective items under each categories , there will be 5 buttons under each categoy. so that I can add a new row for each category.

I basically have an outer gridview and the inner grid (child grid view) is a bulk gridview for which I go the code from

and here is the description abt it

Iam able to display the categories with the items (items will be in the innder bulk grid)  and the button for each row of the category.The problem arises when I click the add new button for any one of the category. It has to add a new row for the category that I clik the add new button.

since I have created a relationships between the data tables I get ... either one of the two happens  either no row gets added or just the blank row remains in that particular category where i clicked the button.

 Hoping for ssome helpp ....



<asp:GridView id="grdDetail" runat="server" autogeneratecolumns="False" showheader="False" OnRowDataBound="grdDetail_RowDataBound" OnRowCommand="grdDetail_RowCommand" >




<table id="ParentTable" cellspacing="0" cellpadding="0" width="100%" border="1" >

   <tr><td >

<asp:Label ID="Label1" runat="server" Text= '<%# Bind("CategoryName") %>' Width="95px"/>




<rwg:BulkEditGridView ID="EditableGrid" AutoGenerateColumns="False"

runat="server" ShowFooter="false" >

<InsertRowStyle BackColor="Cornsilk" />


<asp:TemplateField HeaderText="Description" >


<asp:label id="lblDescription" runat="server" text ='<%#Bind("Description")%>') %></asp:label>



<asp:textbox ID="txtDescription" Width="250px" runat="server" />



<asp:TemplateField HeaderText="Size">


<asp:TextBox id="txtSize" runat="server" text = '<%#Bind("Unit")%>' Width="80px"></asp:TextBox>



<asp:TemplateField HeaderText="Loc 1">


<asp:TextBox id="txtLoc1" runat="server" text = '<%#Bind("CountLoc1")%>' Width="80px"></asp:TextBox>



<asp:TemplateField HeaderText="Loc 2">


<asp:TextBox id="txtLoc2" runat="server" text = '<%#Bind("CountLoc2")%>' Width="80px"></asp:TextBox>



<asp:TemplateField HeaderText="Loc 3">


<asp:TextBox id="txtLoc3" runat="server" text = '<%#Bind("CountLoc3")%>' Width="80px"></asp:TextBox>



<asp:TemplateField HeaderText="Total">


<asp:label id="LblTotal" runat="server" text = '<%#Bind("TotalCount")%>' Width="45px"></asp:label>



<asp:TemplateField HeaderText="Cost this PD">


<asp:TextBox id="txtCost" runat="server" text = '<%#Bind("TotalCount")%>' Width="80px"></asp:TextBox>









<asp:Button ID="btnAddNew" Text="New Item" CommandName="Insert" Runat="server" ></asp:Button>









Code Behind


Public partial class Default1 : System.Web.UI.Page


DataRelation _CategoryRelation;

public DataRelation CategoryRelation


get { return _CategoryRelation; }

set { _CategoryRelation = value; }


protected void Page_Load(object sender, EventArgs e)


// new BulkEditGridView().l




private void BindGrid()


SqlConnection conn = new SqlConnection("Data Source=localhost;Database=Inventory_Latest;Integrated Security=SSPI");

SqlCommand command = new SqlCommand("Usp_ItemsInCategoryForCenter_Get", conn);

command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 52;

command.Parameters.Add("@CenterNumber", SqlDbType.Int).Value = 580;

command.Parameters.Add("@Fiscalyear", SqlDbType.Int).Value = 2008;

command.Parameters.Add("@FiscalPeriod", SqlDbType.Int).Value = 2;

SqlDataAdapter adapter = new SqlDataAdapter(command);

DataSet ds = new DataSet();

adapter.Fill(ds, "Items");


int c = ds.Tables[0].Rows.Count;

ds.Tables[0].TableName = "Categories";

ds.Tables[1].TableName = "ItemsinCategories";

DataColumn Parent = ds.Tables[0].Columns["CategoryId"];

DataColumn Child = ds.Tables[1].Columns["CategoryId"];

_CategoryRelation = new DataRelation("CategoryRelation", Parent, Child, false);


this.grdDetail.DataSource = ds.Tables["Categories"].DefaultView;



protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)


if (e.Row.DataItem != null && e.Row.RowType == DataControlRowType.DataRow)


GridView InnerGrid = (GridView)e.Row.FindControl("EditableGrid");

DataRowView drv = e.Row.DataItem as DataRowView;

DataView dv = drv.CreateChildView(CategoryRelation);

InnerGrid.DataSource = drv.CreateChildView(CategoryRelation);




protected void grdDetail_RowCommand(object sender, GridViewCommandEventArgs e)


if (e.CommandName == "Insert")



GridViewRow mrow = (GridViewRow)((Control)e.CommandSource).NamingContainer;

BulkEditGridView InnerGv = ((BulkEditGridView)mrow.FindControl("EditableGrid"));

InnerGv.InsertRowCount = 3;

InnerGv.EnableInsert = true;


// int index =;


//object Ob = mrow.DataItem;

// // InnerGv.DataSource;

// DataRowView drv = Ob as DataRowView;

// //DataRowView drv = mrow.DataItem as DataRowView;

// DataView dv = drv.CreateChildView(CategoryRelation);

// InnerGv.DataSource = drv.CreateChildView(CategoryRelation);



Question by:Ashwin_shastry
    1 Comment
    LVL 10

    Accepted Solution

    pla refer the following link
    This has Child parent relationship what you want

    Thanks & Regards

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now