PDA

Click to See Complete Forum and Search --> : Multiple Tables in Datagrid


Phenglai
Nov 19th, 2004, 12:34 PM
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


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

pvb
Nov 28th, 2004, 09:57 AM
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/2002/Aug/MasterDetailedDisplay.asp
this one (http://www.standardio.org/article.aspx?id=155) shows an example of nested datagrid. datagridgirl (http://www.datagridgirl.com/articles.aspx) has some links to other examples.