Ashwin_shastry
asked on
adding rows to a child gridview in a nested gridview
Hello,
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 ....
HTML
-------------------------- --------
<asp:GridView id="grdDetail" runat="server" autogeneratecolumns="False " showheader="False" OnRowDataBound="grdDetail_ RowDataBou nd" OnRowCommand="grdDetail_Ro wCommand" >
<columns>
<asp:TemplateField>
<itemtemplate>
<table id="ParentTable" cellspacing="0" cellpadding="0" width="100%" border="1" >
<tr><td >
<asp:Label ID="Label1" runat="server" Text= '<%# Bind("CategoryName") %>' Width="95px"/>
</td>
</tr>
<tr><td>
<rwg:BulkEditGridView ID="EditableGrid" AutoGenerateColumns="False "
runat="server" ShowFooter="false" >
<InsertRowStyle BackColor="Cornsilk" />
<Columns>
<asp:TemplateField HeaderText="Description" >
<EditItemTemplate>
<asp:label id="lblDescription" runat="server" text ='<%#Bind("Description")%> ') %></asp:label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:textbox ID="txtDescription" Width="250px" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Size">
<EditItemTemplate>
<asp:TextBox id="txtSize" runat="server" text = '<%#Bind("Unit")%>' Width="80px"></asp:TextBox >
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loc 1">
<EditItemTemplate>
<asp:TextBox id="txtLoc1" runat="server" text = '<%#Bind("CountLoc1")%>' Width="80px"></asp:TextBox >
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loc 2">
<EditItemTemplate>
<asp:TextBox id="txtLoc2" runat="server" text = '<%#Bind("CountLoc2")%>' Width="80px"></asp:TextBox >
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loc 3">
<EditItemTemplate>
<asp:TextBox id="txtLoc3" runat="server" text = '<%#Bind("CountLoc3")%>' Width="80px"></asp:TextBox >
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<EditItemTemplate>
<asp:label id="LblTotal" runat="server" text = '<%#Bind("TotalCount")%>' Width="45px"></asp:label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost this PD">
<EditItemTemplate>
<asp:TextBox id="txtCost" runat="server" text = '<%#Bind("TotalCount")%>' Width="80px"></asp:TextBox >
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</rwg:BulkEditGridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddNew" Text="New Item" CommandName="Insert" Runat="server" ></asp:Button>
</td>
</tr>
</table>
</itemtemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
-------------------------- ---------- ---------- ---------- ---------- ---------- --
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
if(!Page.IsPostBack)
BindGrid();
}
private void BindGrid()
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Database= Inventory_ Latest;Int egrated Security=SSPI");
SqlCommand command = new SqlCommand("Usp_ItemsInCat egoryForCe nter_Get", conn);
command.CommandType = CommandType.StoredProcedur e;
command.Parameters.Add("@C ategoryID" , SqlDbType.Int).Value = 52;
command.Parameters.Add("@C enterNumbe r", SqlDbType.Int).Value = 580;
command.Parameters.Add("@F iscalyear" , SqlDbType.Int).Value = 2008;
command.Parameters.Add("@F iscalPerio d", SqlDbType.Int).Value = 2;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, "Items");
conn.Close();
int c = ds.Tables[0].Rows.Count;
ds.Tables[0].TableName = "Categories";
ds.Tables[1].TableName = "ItemsinCategories";
DataColumn Parent = ds.Tables[0].Columns["Cate goryId"];
DataColumn Child = ds.Tables[1].Columns["Cate goryId"];
_CategoryRelation = new DataRelation("CategoryRela tion", Parent, Child, false);
ds.Relations.Add(_Category Relation);
this.grdDetail.DataSource = ds.Tables["Categories"].De faultView;
grdDetail.DataBind();
}
protected void grdDetail_RowDataBound(obj ect sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null && e.Row.RowType == DataControlRowType.DataRow )
{
GridView InnerGrid = (GridView)e.Row.FindContro l("Editabl eGrid");
DataRowView drv = e.Row.DataItem as DataRowView;
DataView dv = drv.CreateChildView(Catego ryRelation );
InnerGrid.DataSource = drv.CreateChildView(Catego ryRelation );
InnerGrid.DataBind();
}
}
protected void grdDetail_RowCommand(objec t sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
BindGrid();
GridViewRow mrow = (GridViewRow)((Control)e.C ommandSour ce).Naming Container;
BulkEditGridView InnerGv = ((BulkEditGridView)mrow.Fi ndControl( "EditableG rid"));
InnerGv.InsertRowCount = 3;
InnerGv.EnableInsert = true;
InnerGv.DataBind();
// int index = mrow.data;
//mrow.DataItem;
//object Ob = mrow.DataItem;
// // InnerGv.DataSource;
// DataRowView drv = Ob as DataRowView;
// //DataRowView drv = mrow.DataItem as DataRowView;
// DataView dv = drv.CreateChildView(Catego ryRelation );
// InnerGv.DataSource = drv.CreateChildView(Catego ryRelation );
}
}
}
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 ....
HTML
--------------------------
<asp:GridView id="grdDetail" runat="server" autogeneratecolumns="False
<columns>
<asp:TemplateField>
<itemtemplate>
<table id="ParentTable" cellspacing="0" cellpadding="0" width="100%" border="1" >
<tr><td >
<asp:Label ID="Label1" runat="server" Text= '<%# Bind("CategoryName") %>' Width="95px"/>
</td>
</tr>
<tr><td>
<rwg:BulkEditGridView ID="EditableGrid" AutoGenerateColumns="False
runat="server" ShowFooter="false" >
<InsertRowStyle BackColor="Cornsilk" />
<Columns>
<asp:TemplateField HeaderText="Description" >
<EditItemTemplate>
<asp:label id="lblDescription" runat="server" text ='<%#Bind("Description")%>
</EditItemTemplate>
<InsertItemTemplate>
<asp:textbox ID="txtDescription" Width="250px" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Size">
<EditItemTemplate>
<asp:TextBox id="txtSize" runat="server" text = '<%#Bind("Unit")%>' Width="80px"></asp:TextBox
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loc 1">
<EditItemTemplate>
<asp:TextBox id="txtLoc1" runat="server" text = '<%#Bind("CountLoc1")%>' Width="80px"></asp:TextBox
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loc 2">
<EditItemTemplate>
<asp:TextBox id="txtLoc2" runat="server" text = '<%#Bind("CountLoc2")%>' Width="80px"></asp:TextBox
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loc 3">
<EditItemTemplate>
<asp:TextBox id="txtLoc3" runat="server" text = '<%#Bind("CountLoc3")%>' Width="80px"></asp:TextBox
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<EditItemTemplate>
<asp:label id="LblTotal" runat="server" text = '<%#Bind("TotalCount")%>' Width="45px"></asp:label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost this PD">
<EditItemTemplate>
<asp:TextBox id="txtCost" runat="server" text = '<%#Bind("TotalCount")%>' Width="80px"></asp:TextBox
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</rwg:BulkEditGridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddNew" Text="New Item" CommandName="Insert" Runat="server" ></asp:Button>
</td>
</tr>
</table>
</itemtemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
--------------------------
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
if(!Page.IsPostBack)
BindGrid();
}
private void BindGrid()
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Database=
SqlCommand command = new SqlCommand("Usp_ItemsInCat
command.CommandType = CommandType.StoredProcedur
command.Parameters.Add("@C
command.Parameters.Add("@C
command.Parameters.Add("@F
command.Parameters.Add("@F
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, "Items");
conn.Close();
int c = ds.Tables[0].Rows.Count;
ds.Tables[0].TableName = "Categories";
ds.Tables[1].TableName = "ItemsinCategories";
DataColumn Parent = ds.Tables[0].Columns["Cate
DataColumn Child = ds.Tables[1].Columns["Cate
_CategoryRelation = new DataRelation("CategoryRela
ds.Relations.Add(_Category
this.grdDetail.DataSource = ds.Tables["Categories"].De
grdDetail.DataBind();
}
protected void grdDetail_RowDataBound(obj
{
if (e.Row.DataItem != null && e.Row.RowType == DataControlRowType.DataRow
{
GridView InnerGrid = (GridView)e.Row.FindContro
DataRowView drv = e.Row.DataItem as DataRowView;
DataView dv = drv.CreateChildView(Catego
InnerGrid.DataSource = drv.CreateChildView(Catego
InnerGrid.DataBind();
}
}
protected void grdDetail_RowCommand(objec
{
if (e.CommandName == "Insert")
{
BindGrid();
GridViewRow mrow = (GridViewRow)((Control)e.C
BulkEditGridView InnerGv = ((BulkEditGridView)mrow.Fi
InnerGv.InsertRowCount = 3;
InnerGv.EnableInsert = true;
InnerGv.DataBind();
// int index = mrow.data;
//mrow.DataItem;
//object Ob = mrow.DataItem;
// // InnerGv.DataSource;
// DataRowView drv = Ob as DataRowView;
// //DataRowView drv = mrow.DataItem as DataRowView;
// DataView dv = drv.CreateChildView(Catego
// InnerGv.DataSource = drv.CreateChildView(Catego
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.