yvsupport
asked on
Drill-Down DataGrid with Nested DataGrids
I am trying to create a datagrid that dynamically populates it cells with nested datagrids. The solution works on the idea that clicking on the select column of the master datagrid will dynamically insert the child data into another datagrid within one of the selected rows cells. This works fine when drilling down one layer. I need the select button on the nested grid to create another grid within its own grid. However, when I click on the nested select button the whole master datagrid collapses. It seems that I need to be able to keep the master grid expanded on postback. I am fairly new to .Net so I'm not sure that what I am attempting is even possible. Any help or better alternatives will be greatfully received.
Here is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
namespace YVPersonnel
{
public class uiCompanyMaintenence3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls. DataGrid dgDivision;
protected System.Web.UI.WebControls. DataGrid dgSite;
private static string cns = "Data Source=localhost;Initial Catalog=YVPersonnel;Integr ated Security=SSPI";
private SqlConnection cn = new SqlConnection(cns);
protected System.Web.UI.WebControls. TextBox TextBox1;
protected System.Web.UI.WebControls. TextBox TextBox2;
public int divisionID;
public int siteID;
private void Page_Load(object sender, System.EventArgs e)
{
dgDivision.SelectedIndex = Convert.ToInt32(Cache["div isionIndex "]);
if(!Page.IsPostBack)
{
getDivisionDataSet();
}
}
private void getDivisionDataSet()
{
beDivision oDivision;
oDivision = new beDivision();
dgDivision.DataSource = oDivision.getDivisionDataS et(cn);
dgDivision.DataBind();
}
private DataGrid createSiteDataGrid(int divisionID)
{
SqlCommand cmd = new SqlCommand("select * from yvsite where divisionid = " + divisionID, cn);
DataGrid dgSite = new DataGrid();
dgSite.AutoGenerateColumns = false;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
dgSite.DataSource = ds;
EditCommandColumn Edit = new EditCommandColumn();
Edit.EditText = "Edit";
TemplateColumn Select = new TemplateColumn();
Select.ItemTemplate = new DataGridTemplate(ListItemT ype.Select edItem, "Select");
BoundColumn SiteID = new BoundColumn();
SiteID.HeaderText = "SiteID";
SiteID.DataField = "siteid";
BoundColumn Site = new BoundColumn();
Site.HeaderText = "Site";
Site.DataField = "shortName";
TemplateColumn newDG = new TemplateColumn();
dgSite.Columns.AddAt(0, Select);
dgSite.Columns.AddAt(1, SiteID);
dgSite.Columns.AddAt(2, Site);
dgSite.Columns.AddAt(3, newDG);
dgSite.Columns.AddAt(4, Edit);
dgSite.Columns[1].Visible = false;
dgSite.DataBind();
cn.Close();
dgSite.SelectedIndexChange d += new System.EventHandler(dgSite _SelectedI ndexChange d);
return dgSite;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgDivision.SelectedIn dexChanged += new System.EventHandler(this.d gDivision_ SelectedIn dexChanged );
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
private void dgDivision_SelectedIndexCh anged(obje ct sender, System.EventArgs e)
{
divisionID = Convert.ToInt32(dgDivision .SelectedI tem.Cells[ 1].Text);
DataGrid dgSite = createSiteDataGrid(divisio nID);
dgDivision.SelectedItem.Ce lls[3].Con trols.AddA t(0, dgSite);
if(dgDivision.SelectedInde x > 3)
{
dgDivision.Enabled = false;
dgSite_SelectedIndexChange d(this.dgS ite, e);
}
}
private void dgSite_SelectedIndexChange d(object sender, System.EventArgs e)
{
TextBox1.Text = "Bugger!";
}
}
}
Cheers!
Here is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
namespace YVPersonnel
{
public class uiCompanyMaintenence3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
private static string cns = "Data Source=localhost;Initial Catalog=YVPersonnel;Integr
private SqlConnection cn = new SqlConnection(cns);
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
public int divisionID;
public int siteID;
private void Page_Load(object sender, System.EventArgs e)
{
dgDivision.SelectedIndex = Convert.ToInt32(Cache["div
if(!Page.IsPostBack)
{
getDivisionDataSet();
}
}
private void getDivisionDataSet()
{
beDivision oDivision;
oDivision = new beDivision();
dgDivision.DataSource = oDivision.getDivisionDataS
dgDivision.DataBind();
}
private DataGrid createSiteDataGrid(int divisionID)
{
SqlCommand cmd = new SqlCommand("select * from yvsite where divisionid = " + divisionID, cn);
DataGrid dgSite = new DataGrid();
dgSite.AutoGenerateColumns
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
dgSite.DataSource = ds;
EditCommandColumn Edit = new EditCommandColumn();
Edit.EditText = "Edit";
TemplateColumn Select = new TemplateColumn();
Select.ItemTemplate = new DataGridTemplate(ListItemT
BoundColumn SiteID = new BoundColumn();
SiteID.HeaderText = "SiteID";
SiteID.DataField = "siteid";
BoundColumn Site = new BoundColumn();
Site.HeaderText = "Site";
Site.DataField = "shortName";
TemplateColumn newDG = new TemplateColumn();
dgSite.Columns.AddAt(0, Select);
dgSite.Columns.AddAt(1, SiteID);
dgSite.Columns.AddAt(2, Site);
dgSite.Columns.AddAt(3, newDG);
dgSite.Columns.AddAt(4, Edit);
dgSite.Columns[1].Visible = false;
dgSite.DataBind();
cn.Close();
dgSite.SelectedIndexChange
return dgSite;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgDivision.SelectedIn
this.Load += new System.EventHandler(this.P
}
#endregion
private void dgDivision_SelectedIndexCh
{
divisionID = Convert.ToInt32(dgDivision
DataGrid dgSite = createSiteDataGrid(divisio
dgDivision.SelectedItem.Ce
if(dgDivision.SelectedInde
{
dgDivision.Enabled = false;
dgSite_SelectedIndexChange
}
}
private void dgSite_SelectedIndexChange
{
TextBox1.Text = "Bugger!";
}
}
}
Cheers!
ASKER
The nested grid will not submit - when you click the select link, the outer grids selectedIndexChanged event gets fired rather than the inner grid's. This clears the inner datagrid from the outer. The Cache["divisionIndex"] value line should have been removed.
Cheers.
Cheers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think the problem is more connected that on submit of the nested datagrid you do not re-create it.
You must recreate the dynamic created controls every time after the first creation
Also where do you change the Cache["divisionIndex"] value ?
Cheers!