• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2179
  • Last Modified:

adding rows to a child gridview in a nested gridview


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 http://www.codeplex.com/ASPNetRealWorldContr/SourceControl/ListDownloadableCommits.aspx

and here is the description abt it  http://blogs.msdn.com/mattdotson/articles/490868.aspx

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 = mrow.data;


//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);



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

Thanks & Regards
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now