Gallery

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am trying to create a gallery (n columns by m rows) of images.

I am having a few problems on the steps.

From a List<Product> I am doing the following:

1. Determine the number of rows needed. I am using:

Int32 rows = (Int32)Math.Ceiling(products.Count() / (Double)
(columns == 0 ? 1 : columns));

I think it is ok ...

2. Loop through all products and create the rows and columns. For
example:

If I have 10 records and 4 columns I will have 3 rows.

Then I will need to loop through all products doing as follows:

Create row 1:
Create columns 1,2,3,4 with Product 1,2,3,4
Create row 2
Create columns 1,2,3,4 with Product 5,6,7,8
Create row 3:
Create columns 1,2 with Products 9,10 and create columns 3,4
with String.Empty.

My problem is to create this loop and using the List<Product>.
I will need some kind of index ... I think. I am not sure I can do
this with a List.

How can I do this?

Thanks,
Miguel
 
shapper said:
If I have 10 records and 4 columns I will have 3 rows.

Then I will need to loop through all products doing as follows:

Create row 1:
Create columns 1,2,3,4 with Product 1,2,3,4
Create row 2
Create columns 1,2,3,4 with Product 5,6,7,8
Create row 3:
Create columns 1,2 with Products 9,10 and create columns 3,4
with String.Empty.

My problem is to create this loop and using the List<Product>.
I will need some kind of index ... I think. I am not sure I can do
this with a List.

How can I do this?

If you already have computed the number of rows and columns, you can
nest two loops and index the List with an operation between both indices:

for (int i=0; i<rows; i++)
{
//...create row i...
for (int j=0; j<columns; j++)
{
//...create column j
int index = i*columns+j;
if (index>=products.Count)
//Create empty cell
else
{
Product p = products[index];
//Create cell with p
}
}
}
 
Back
Top