Click here to Skip to main content
14,668,658 members
Rate this:
Please Sign up or sign in to vote.
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'/>";
                                   dieCutRow.Cells.Add(dieCutCell);

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


                                  tblDieCuts.Rows.Add(dieCutRow);
                       }


If I take the
dieCutRow = new TableRow();
and
tblDieCuts.Rows.Add(dieCutRow);

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.
Posted
Comments
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; https://msdn.microsoft.com/EN-US/library/27y4ybxw(v=VS.100,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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100