Displaying a DataTable within a DataGrid cell ASP.NET

M

mr killer

I have an ASP.NET application simply displaying a DataGrid control on a form.
Within some of the DataGrid cells I display multiple records within that
cell based on an SQL query that loops through some table records.
Unfortunately I can't format those records to be on there own separate line
within the DataGrid cell, because the cell obviously just displays my results
one after the other in the same cell. What I want to do is populate a
separate DataTable, and store each record I grab in my query into its' own
row within that DataTable. Then I want to display that DataTable within a
specified DataGrid cell so the data will look stacked within the cell. This
stems of the fact that HTML developers use tables for positioning data on a
web-form, so I want to see if I can do the same thing within an ASP.NET
DataGrid control. Is this possible? Thanks.
 
T

TenDot

Killer,

I'm not a great expert but one way to do this would be to pull 2 queries,
one for your parent table and one for your child table. Load them both into
the same dataset so you've got ds.Tables[0] and ds.Tables[1]. Then build a
relationship between the tables. When you put that into a grid you get a
list of parent records with a button that will show the child records.
Something like this:

DataSet dsDD = new DataSet( "CPRdd" );
string sqlCmd = "Select * from cprTables";
SqlDataAdapter da = new SqlDataAdapter( sqlCmd, this._cnxnCPRdd );
/// set up the tables in the DataSet
da.Fill( dsDD, "Tables" );
da.SelectCommand.CommandText = "Select * from cprFields order by FldName";
da.Fill( dsDD, "Fields" );
da.SelectCommand.CommandText = "Select * from TblComments order by
TblName";
da.Fill( dsDD, "TblCmts" );
da.SelectCommand.CommandText = "Select * from FldComments order by
FldName";
da.Fill( dsDD, "FldCmts" );

/// -----------------------------------------------------------
/// build the relations for each table:
/// -----------------------------------------------------------
/// Tables table to Fields
dsDD.Relations.Add( "TableFields",
dsDD.Tables["Tables"].Columns["Pk_table"],dsDD.Tables["Fields"].Columns["TblKey"]);
/// relate Comments to Tables
dsDD.Relations.Add( "TableComment",
dsDD.Tables["Tables"].Columns["TblName"],dsDD.Tables["TblCmts"].Columns["TblName"]);

I hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top