Click here to Skip to main content
14,735,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a dynamic table with back end code using Visual Studio and C#. The problem that I have is that I either get each record in an individual row or all records in one row. I would like to use say a for statement to put say 3 to 4 items in a row, then start a new row. I have tried changing the code multiple times, but still don't get it to appear they way I want it to look.

This puts each image in an individual row.

while (theReader.Read())
                                   dieCutRow = new TableRow();

                                   string dieName = theReader["DieName"].ToString();
                                   string dieImage = theReader["Image"].ToString();
                                   string dieDirectory = theReader["DieType"].ToString();
                                   string dieURL = "images/" + dieDirectory + "/" + dieImage;

                                   dieCutCell = new TableCell();
                                   dieCutCell.Style.Add("width", "100px");
                                   dieCutCell.CssClass += " cartPaddingLeft";
                                   dieCutCell.Text = "<img src='images/" + dieDirectory + "/" + dieImage + "' width='100'/>";

                                   dieCutCell = new TableCell();
                                   dieCutCell.Style.Add("width", "100px");
                                   dieCutCell.CssClass += " cartPaddingLeft";
                                   dieCutCell.Text = "" + theReader["DieName"].ToString() + " ";


If I take the
dieCutRow = new TableRow();

move them outside the while loop, then I get a single row with all of my data in it.

What I have tried:

I have tried adding a for loop, etc. I thought about a SortedList. A gridview basically gives me what I currently have with the existing code.
an0ther1 3-Oct-17 17:38pm
If you want to put "3 or 4" items into a single row then you will need another loop to add the data to your Table Cells.

theReader.Read() will read the next block of data - a single row.
If you want to put more than one row into your Cells you need to call theReader.Read() again to get more data.

Based on what you have provided, I would suggest you need to use a Dataset & DataTable instead of a DataReader - refer MSDN documentation;,d=hv.2).aspx
Specifically read the section on Choosing a DataReader or Dataset
I would also suggest you need to re-think your design

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900