Results 1 to 2 of 2

Thread: Multiple Tables in Datagrid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262

    Multiple Tables in Datagrid

    Does anyone know if it is possible to add multiple tables into a datagrid in an asp.net page? I know it can be done in a regular .Net datagrid but can it be done in ASP.Net?

    I am trying to set up a a grid where the end user can drill down through the information and I need to have a few tables assigned to the datagrid.

    Here is the code I have so far

    Code:
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			DataSet mainDS = new DataSet("Main");
    
    			BuildDepartmentTable(ref mainDS);
    			BuildEmployeeTable(ref mainDS);
    
    			BuildRelationships(ref mainDS);
    			
    			dgPhone.DataSource = mainDS;
    			//dgPhone.DataMember = "Main";
    			dgPhone.DataBind();
    			dgPhone.Visible = true;
    			dgPhone.Font.Size = 8;
    
    		}
    
    		private void BuildEmployeeTable(ref DataSet mainDS)
    		{
    
    			string conn = "";
    			string cmd = "";
    
    			cmd = "select fname, lname, department from employee";
        
    			conn = System.Configuration.ConfigurationSettings.AppSettings["connSql"];
    			DataTable dt = new DataTable("Employee");
        
    			SqlDataAdapter tAdpt = new SqlDataAdapter(cmd, conn);
        
    			tAdpt.Fill(dt);
    
    			mainDS.Tables.Add(dt);
    
    			tAdpt.Dispose();
    		}
    
    		private void BuildDepartmentTable(ref DataSet mainDS)
    		{
    
    			string conn = "";
    			string cmd = "";
    
    			cmd = "select deptid, deptname from department";
        
    			conn = System.Configuration.ConfigurationSettings.AppSettings["connSql"];
    			DataTable dt = new DataTable("Department");
        
    			SqlDataAdapter tAdpt = new SqlDataAdapter(cmd, conn);
        
    			tAdpt.Fill(dt);
    
    			mainDS.Tables.Add(dt);
    
    			tAdpt.Dispose();
    		}
    
    		private void BuildRelationships(ref DataSet mainDS)
    		{
    			DataRelation dr = new DataRelation("EmpDept", 
    				mainDS.Tables["Department"].Columns["deptid"],
    				mainDS.Tables["Employee"].Columns["department"]);
    			mainDS.Relations.Add(dr);
    		}
    Jerel

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    You don't get that behavior out-of-the box with the ASP.NET datagrid but you can roll it yourself. One technique I've used is nesting one datagrid(the details) inside another(master). Then dynamically bind the details at runtime. Here's a link to the article I used, the author uses a repeater in the example but it's the same concept: http://www.c-sharpcorner.com/Code/20...ledDisplay.asp
    this one shows an example of nested datagrid. datagridgirl has some links to other examples.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width