0

I have a method which returns a List<string> which my code uses to populate a table:

string[] siteNames;
siteNames = client.GetListSiteElementNames();

foreach (string siteName in siteNames)
{
    TableCell tempCell = new TableCell();
    tempCell.Text = siteName;
    this.sitesTableRow.Cells.Add(tempCell);
}

This inserts the cells on to a single row, so the cells are presented horizontally across the screen (how I would like), but because they are 20 or so of them, they go off the page meaning you need to scroll across to view the rest.

Is there any way I can do this so that the cells cut off at the edge of the window and onto a new line?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Phil
  • 563
  • 4
  • 6
  • 16

1 Answers1

0

To do this you will need to add a new row after a certain cut off point (i.e. certain number of columns)

In the example below I use a cut off of 5 :

string[] siteNames;
siteNames = client.GetListSiteElementNames();

// Number of cells on each row
int cutOffPoint = 5;
int currentNum = 1;

foreach (string siteName in siteNames) {
    TableCell tempCell = new TableCell();
    tempCell.Text = siteName;

    if (currentNum > cutOffPoint) {
        currentNum = 1;

        // Code to add new row to table
    }    

    this.sitesTableRow.Cells.Add(tempCell);

    currentNum++;
}

Because I'm not sure what the this object is referring to or the sitesTableRow I can't include the code to add the row, but the idea would be that after 6 cells you would reset the currentNum variable and add a new row, and append the cells to this new row.

Nunners
  • 3,047
  • 13
  • 17
  • I did consider this way before but I wanted the cells to wrap when the window was resized. This is probably the simplest way though. Was wondering if there is any css styling you could do. – Phil Feb 19 '14 at 14:18
  • @PhilipLoffler For this you could use something other than a Table, you could create a HTML string of floating `DIV` elements, that would enable you to wrap them on re-size. – Nunners Feb 19 '14 at 14:22
  • @Phil Look at [this](http://stackoverflow.com/questions/11250501/wrap-long-html-tables-to-next-line) for an example. – Nunners Feb 19 '14 at 14:23
  • This could work, but not sure how to implement this because those images are declared statically. – Phil Feb 19 '14 at 14:27
  • @Phil How are you adding the Table to the page? All you'll have to do is create a `HtmlGenericControl` and set the `InnerText` to be the siteName, you can then add the `attribute` of `class` to this control and add it to the same control you are adding your table to (instead of the table). – Nunners Feb 19 '14 at 14:41