troubleshooting Question

adding rows to a child gridview in a nested gridview

Avatar of Ashwin_shastry
Ashwin_shastry asked on
.NET ProgrammingC#
1 Comment1 Solution2385 ViewsLast Modified:

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



Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros