private DataTable GetParentTableData()
{
DataTable table = new DataTable();
table.Columns.Add("EmployeeId", typeof(string));
table.Columns.Add("EmployeeName", typeof(string));
table.Columns.Add("Designation", typeof(string));
table.Columns.Add("Location", typeof(string));
table.Rows.Add(100, "Andy", "S/W Engg", "NY");
table.Rows.Add(200, "James", "S/W Engg", "NJ");
table.Rows.Add(300, "Ramesh", "Sr. S/W Engg", "Bangalore");
table.Rows.Add(400, "George", "Architect", "London");
table.Rows.Add(500, "Lisa", "Manager", "Washington");
return table;
}
protected void Page_Load(object sender, EventArgs e)
{
gvParent.DataSource = GetParentTableData();
gvParent.DataBind();
if (!Page.IsPostBack)
{
GridViewChildPageIndex();
}
}
Step 7: Create a method GetChildTableData which will return datatable. The datatable returned by GetChildTableData will bind to child gridview. EmployeeId of parent and childgridview will be same. It will act as foreign key.
private DataTable GetChildTableData()
{
DataTable table = new DataTable();
table.Columns.Add("SkillID", typeof(string));
table.Columns.Add("EmployeeId", typeof(string));
table.Columns.Add("SkillSet", typeof(string));
table.Columns.Add("Remarks", typeof(string));
table.Rows.Add("1", "100", "ASP.NET", "Remarks1");
table.Rows.Add("2", "100", "SQL", "Remarks2");
table.Rows.Add("1", "200", "ASP.NET", "Remarks1");
table.Rows.Add("2", "200", "SQL", "Remarks2");
table.Rows.Add("3", "200", "WCF", "Remarks3");
table.Rows.Add("4", "300", "PHP", "Remarks1");
table.Rows.Add("5", "300", "Oracle", "Remarks2");
table.Rows.Add("6", "300", "Javascipt", "Remarks3");
table.Rows.Add("7", "400", "J2EE", "Remarks1");
table.Rows.Add("5", "400", "Oracle", "Remarks2");
table.Rows.Add("8", "400", "Struts", "Remarks3");
table.Rows.Add("9", "500", "Estimation", "Remarks1");
table.Rows.Add("10", "500", "Project Plan", "Remarks2");
table.Rows.Add("11", "500", "Resource Loading", "Remarks3");
table.Rows.Add("12", "500", "Resource allocation", "Remarks4");
return table;
}
Step 8: Add gvParent_RowDataBound method, in this method the child grid view will be searched in the parent gridview and corresponding data will bind to it.
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.Cells[0].FindControl("img1");
Literal ltrl = (Literal)e.Row.FindControl("lit1");
ltrl.Text = ltrl.Text.Replace("trCollapseGrid", "trCollapseGrid" + e.Row.RowIndex.ToString());
string str = "trCollapseGrid" + e.Row.RowIndex.ToString();
e.Row.Cells[0].Attributes.Add("OnClick", "OpenTable('" + str + "','" + img.ClientID + "')");
e.Row.Cells[0].RowSpan = 1;
System.Web.UI.WebControls.GridView gvChild = (System.Web.UI.WebControls.GridView)e.Row.FindControl("gvChild");
if (Session["ChildPageIndex"] != null)
{
DataTable dtPageIndex = (DataTable)Session["ChildPageIndex"];
gvChild.PageIndex = Convert.ToInt16(dtPageIndex.Rows[e.Row.RowIndex][0]);
}
BindChildGrdView(gvParent.DataKeys[e.Row.RowIndex].Value.ToString(), gvChild);
}
}
Step 9: Add gvChild_PageIndexChanging method which will provide functionality of pagin in child gridview. I have added System.Threading.Thread.Sleep to make it appear for some time. Remove it in real implementation in the application.
protected void gvChild_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
System.Threading.Thread.Sleep(2000);
System.Web.UI.WebControls.GridView gvwChild = ((System.Web.UI.WebControls.GridView)sender);
GridViewRow gvRowParent = ((System.Web.UI.WebControls.GridView)sender).Parent.Parent as GridViewRow;
gvwChild.PageIndex = e.NewPageIndex;
if (Session["ChildPageIndex"] != null)
{
DataTable dtPageIndex = (DataTable)Session["ChildPageIndex"];
dtPageIndex.Rows[gvRowParent.RowIndex][0] = e.NewPageIndex;
}
BindChildGrdView(gvParent.DataKeys[gvRowParent.RowIndex].Value.ToString(), gvwChild);
if (!ClientScript.IsStartupScriptRegistered("alert"))
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "document.getElementById('" + hidRowId.Value + "').style.display = ';", true);
}
}
Session has been used in gvParent_RowDataBound and gvChild_PageIndexChanging method to maintain the paging index of each child grid.
Step 10: GridViewChildPageIndex method is used to set the pageindex to 0 in the session object.
private void GridViewChildPageIndex()
{
DataTable dtPageIndex = new DataTable();
dtPageIndex.Columns.Add("PageIndex", typeof(int));
for (int i = 0; i < gvParent.Rows.Count; i++)
{
dtPageIndex.Rows.Add("0");
}
Session["ChildPageIndex"] = dtPageIndex;
}
Step 11: BindChildGrdView will bind the data to the child gridview.
private void BindChildGrdView(string empId, System.Web.UI.WebControls.GridView gvChild)
{
DataTable dtChildTable = GetChildTableData();
DataTable dtCloneChildTable = dtChildTable.Clone();
DataRow[] gvChildRows = dtChildTable.Select("EmployeeId = " + empId);
foreach (DataRow gvChildRow in gvChildRows)
{
dtCloneChildTable.ImportRow(gvChildRow);
}
gvChild.DataSource = dtCloneChildTable;
gvChild.DataBind();
}
This ends the article of paging in nested gridview.