Link to home
Start Free TrialLog in
Avatar of Ashwin_shastry
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_RowDataBound" OnRowCommand="grdDetail_RowCommand" >

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

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["CategoryId"];

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

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

ds.Relations.Add(_CategoryRelation);

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

grdDetail.DataBind();

}

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

InnerGrid.DataBind();




}


}



protected void grdDetail_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "Insert")

{

BindGrid();

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

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

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

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

 }

}

}
ASKER CERTIFIED SOLUTION
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial