Solved

Drill-Down DataGrid with Nested DataGrids

Posted on 2006-11-10
7
453 Views
Last Modified: 2012-06-27
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;Integrated 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["divisionIndex"]);

                  if(!Page.IsPostBack)
                  {
                        getDivisionDataSet();
                  }
            }

            private void getDivisionDataSet()
            {
                  beDivision oDivision;
                  
                  oDivision = new beDivision();

                  dgDivision.DataSource = oDivision.getDivisionDataSet(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(ListItemType.SelectedItem, "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.SelectedIndexChanged += new System.EventHandler(dgSite_SelectedIndexChanged);
                  
                  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.SelectedIndexChanged += new System.EventHandler(this.dgDivision_SelectedIndexChanged);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion


            private void dgDivision_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  divisionID = Convert.ToInt32(dgDivision.SelectedItem.Cells[1].Text);

                  DataGrid dgSite = createSiteDataGrid(divisionID);

                  dgDivision.SelectedItem.Cells[3].Controls.AddAt(0, dgSite);

                  if(dgDivision.SelectedIndex > 3)
                  {
                        dgDivision.Enabled = false;
                        dgSite_SelectedIndexChanged(this.dgSite, e);
                  }
            }


            private void dgSite_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  TextBox1.Text = "Bugger!";      
            }      
      }
}



Cheers!
0
Comment
Question by:yvsupport
  • 2
7 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 17914426
Hi yvsupport,

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!
0
 

Author Comment

by:yvsupport
ID: 17914519
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.
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 250 total points
ID: 17914592
yvsupport,
It will be easier if you create child DataGrid in a template column, and use Parent grid's SelectedIndexChange event to just load data into Child datagrid. Then if you want to attach new event for ChildDatagrid then you'll have to specify that in child datagrid declaration at ASPX page.

mmarinov, long time! hope you are fine..

-tushar
0
 
LVL 28

Assisted Solution

by:mmarinov
mmarinov earned 250 total points
ID: 17928417
yvsupport,

The page is submitted so the selectedIndexChanged is rised. But tushar is much closer to the solution than me - create the datagrid on design time an not dynamic and you will not have to problem i'm talking about.

tushar-> yes, it was a very long year :) but i fine, thanks - hope your are fine too

regards
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question