Thursday, March 22, 2012

Looping Data into tables

Hi all,

I am writing an order management system for our company. it is currently in phase 1 which is to get something usable in for the users to capture data. currently when the user clicks on a line in my gridview containing a specific orders summary, it takes the user to another page which displays the order in detail. there are 3 columns in the first row with client details, sales person details etc and the second row i have slapped in a iframe.

here is where i am stuck. i am sending the orderID with the src in the iframe and i want to pull back all of the lines (details) associated with the orderID. there could be 1 or many lines. what i want to happen is all of these need to be displayed neatly 1 after the other in sort of mini reports, like a table where all data is structured to make things more readable, the thing is i dont know where to start. I can obviously pull back all the data i need in a recordset and then loop through the data, but how do i create the associated table template?

Using this website as an example, when you click on my posts at the top of the page it displays all of my posts 1 after another which are associated with me.

any ideas?

thanks

Bryan

Looping was the ASP way - not .Net

Either use a gridview or a repeater -

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/data/databases.aspx#gridview

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/repeater.aspx


There are several ways in which you can find a solution to your issue. First you can use a repeater(http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx) and define how you want your rows to be rendered. But if you want for display only I would recommend you use a HtmlTable instead. This is a very "light object"

At a high level your code would look like this below and you can see you have complete control of how you want to render your table abnd you can add more logic to comform to the way you want to render your page.

Say you have a DataTable which you populate items from a particular order lets called dtOrderDetails

HtmlTable tbOrderDetails=new HtmlTable();
HtmlTableRow tr=new HtmlTableRow();
HtmlTableCell tc;
//Create HeaderRow
foreach(DataColumn c in dtOrderDetails.Columns)
{
tc=new HtmlTableCell();
tc.Attributes["class"]="Give the Css Header class here"
tc.InnerHtml=c.ColumnName
tr.Cells.Add(tc);
}
tbOrderDetails.Rows.Add(tr)

//Create Rows

foreach(DataRow r in dtOrderDetails.Rows
{
tr=new HtmlTableRow();
foreach(DataColumn cr in dtOrderDetails.Columns)
{

tc=new HtmlTableCell();
tc.Attributes["class"]="Give the css class here"
tc.InnerHtml=r[cr.ColumnName];
tr.Cells.Add(tc);
}
tbOrderDetails.Rows.Add(tr);

}
Page.Controls.Add(tbOrderDetails);


Thanks, I used a repeater. Exctally what a was looking for.


cool!

0 comments:

Post a Comment