I want to display items as follows,
我想显示如下项目,
<td1> <td2> <td3> <td4>
1 7 13 19
2 8 14 20
3 9 15 21
4 10 16 22
5 11 17 23
6 12 18
I am getting data (from 1......23) in a single column from database. Now I want to run a loop which will display my single columns data in the above format. Please tell me the for loop code using which I can display data in above format. Data can be more that 23 in production environment, So the logic should be as that it can handle any amount of data. I am using ASP.NET(C#).
我从数据库的单个列中获取数据(从1 ...... 23开始)。现在我想运行一个循环,它将以上述格式显示我的单列数据。请告诉我for循环代码,我可以使用它来显示上述格式的数据。生产环境中的数据可能超过23,因此逻辑应该可以处理任何数量的数据。我正在使用ASP.NET(C#)。
Thanks
3 个解决方案
#1
OK, here's a corrected version of Riho's loop:
好的,这是Riho循环的修正版本:
int records = ... ; /* the number of records */
int cols = 4; /* the number of columns */
int rows = (records + cols - 1) / cols; /* nb: assumes integer math */
for (int row = 0; row < rows; ++row) {
print "<tr>";
for (int col = 0; col < cols; ++col) {
print "<td>";
int offset = col * rows + row;
if (offset < records) {
print data[offset];
} else {
print "nbsp;" /* nb: should have an & but markdown doesn't work */
}
print "</td>";
}
print "</tr>";
}
The
cell is often necessary to ensure that the rendered HTML cell has the correct background. Missing cells or cells with no data in them aren't rendered the same as normal cells.
通常需要单元格来确保呈现的HTML单元格具有正确的背景。丢失的细胞或没有数据的细胞不会与正常细胞相同。
#2
I think you could use asp:DataList and bind the data to it.
Here is an example of using datalist with RepeatDirection and RepeatColumns properties.
我想你可以使用asp:DataList并将数据绑定到它。以下是使用具有RepeatDirection和RepeatColumns属性的datalist的示例。
#3
In pseudocode (didn't test it):
在伪代码中(没有测试它):
int recordnum=....; //get the total number of records
int col_length=recordnum/4;
for(int i=0;i<col_length;i++)
{ for(int j=0;j<4;j++)
print data[i+j*col_length] ;
print "\n";
}
#1
OK, here's a corrected version of Riho's loop:
好的,这是Riho循环的修正版本:
int records = ... ; /* the number of records */
int cols = 4; /* the number of columns */
int rows = (records + cols - 1) / cols; /* nb: assumes integer math */
for (int row = 0; row < rows; ++row) {
print "<tr>";
for (int col = 0; col < cols; ++col) {
print "<td>";
int offset = col * rows + row;
if (offset < records) {
print data[offset];
} else {
print "nbsp;" /* nb: should have an & but markdown doesn't work */
}
print "</td>";
}
print "</tr>";
}
The
cell is often necessary to ensure that the rendered HTML cell has the correct background. Missing cells or cells with no data in them aren't rendered the same as normal cells.
通常需要单元格来确保呈现的HTML单元格具有正确的背景。丢失的细胞或没有数据的细胞不会与正常细胞相同。
#2
I think you could use asp:DataList and bind the data to it.
Here is an example of using datalist with RepeatDirection and RepeatColumns properties.
我想你可以使用asp:DataList并将数据绑定到它。以下是使用具有RepeatDirection和RepeatColumns属性的datalist的示例。
#3
In pseudocode (didn't test it):
在伪代码中(没有测试它):
int recordnum=....; //get the total number of records
int col_length=recordnum/4;
for(int i=0;i<col_length;i++)
{ for(int j=0;j<4;j++)
print data[i+j*col_length] ;
print "\n";
}