在过去的几天里,这一直在伤害我的大脑,我对C#还很陌生。事实证明,这是一个相当大的挑战。
我需要的最终结果是为2D数组中的任意给定列数和值数填充一个垂直排序的HTML表(数组已经按顺序排列)。例如:
|01|07|13|19|25|31|37|43|
|02|08|14|20|26|32|38|44|
|03|09|15|21|27|33|39| |
|04|10|16|22|28|34|40| |
|05|11|17|23|29|33|41| |
|06|12|18|24|30|36|42| |
仅在最后一列中有空格。
我刚刚删除了我的帖子,因为我事后想到,如果它是这样的,也可以接受:
|01|07|13|19|25|30|35|40|
|02|08|14|20|26|31|36|41|
|03|09|15|21|27|32|37|42|
|04|10|16|22|28|33|38|43|
|05|11|17|23|29|34|39|44|
|06|12|18|24| | | | |
只有最后一行有空单元格。(然而,我的代码目前正在使用前面的示例……这可能是问题所在。)
我的代码如下:
int columnCount = 8;
int index = 0;
int tdcount = 0;
int trcount = 1;
String arrayhtml2 = "<table><tr>";
int numStatuses = Enumerable.Range(0, statuses.GetLength(0)).Count(i => statuses[i, 0] != null); //counts not null rows in array
//numStatuses = 28; //14, 21, multiples of 7 are all weird, and 18
double rowCount = Math.Ceiling(Convert.ToDouble(numStatuses) / Convert.ToDouble(columnCount)); //gets total number of rows including blanks
for (int i = 0; i < rowCount * columnCount; i++)
{
index = Convert.ToInt32((i % columnCount) * rowCount + Math.Floor(Convert.ToDouble(i) / (columnCount))); //determines index number to print to that cell
if (index < numStatuses) //determines if a row should be populated or blank
{
arrayhtml2 += "<td>" + statuses[index, 0] + "</td>";
}
else
{
//blank
arrayhtml2 += "<td> </td>";
}
tdcount++; //counts to figure out when to make new row
if (tdcount >= columnCount && trcount != rowCount)
{
trcount++;
arrayhtml2 += "</tr><tr>";
tdcount = 0;
}
}
arrayhtml2 += "</tr></table>";
statusbox.Text = arrayhtml2;
基本上它是有效的,但是当您将numStatues(我已经注释掉了它)设置为7的倍数(这最终使最后一列为空)或18的倍数(最终使6列为3行)时,它的行为会很奇怪。
此外,我省略了一些代码,使其更易于查看。我使用2D数组来确定将什么颜色设置为单元格的背景。
如果代码很难看,我深表歉意。。。我不是最好的程序员。
明白了。这是后代的要点。
int numStatuses = Enumerable.Range(0, statuses.GetLength(0)).Count(i => statuses[i, 0] != null); //counts not null rows in array
int numColumns = 9;
int remainder = numStatuses % numColumns;
int floorRows = Convert.ToInt32(Math.Floor(Convert.ToDecimal(numStatuses) / Convert.ToDecimal(numColumns)));
int ceilingRows = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(numStatuses) / Convert.ToDecimal(numColumns)));
int numRows = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(numStatuses) / Convert.ToDouble(numColumns)));
int colCount = 0;
int index = 0;
double colWidth = Math.Round((100.00 / numColumns), 2);
String statusHTML = null;
statusHTML = "<table width="100%">n<tr>n<td width="" + colWidth + "%">";
for (int i = 0; i < numColumns; i++) // runs for the total number of columns
{
if (index < numStatuses) // doesn't print blank array values
{
if (colCount < remainder) // columns that go down all the way
{
for (int j = 0; j < ceilingRows; j++)
{
statusHTML += "<div class="" + statuses[index, 1] + "">" + statuses[index, 0] + "</div>";
index++;
}
}
else // columns that don't go down all the way
{
for (int j = 0; j < floorRows; j++)
{
statusHTML += "<div class="" + statuses[index, 1] + "">" + statuses[index, 0] + "</div>";
index++;
}
}
colCount++;
if (colCount < numColumns && colCount < numStatuses) // checks if done with column
{
statusHTML += "</td><td width="" + colWidth + "%">";
}
else if (colCount == numColumns)
{
colCount = 0;
}
}
}
if (numStatuses < numColumns) // if it's only one row
{
for (int j = 0; j < numColumns - numStatuses; j++)
{
statusHTML += "</td>n<td width="" + colWidth + "%">";
}
}
statusHTML += "</td>n</tr>n</table>";
}
编辑:注意:我实际上是在骗它看起来像一个表,但实际上我是在加载一个带有div元素的表,这样我就可以一次加载一列,而不必跳过索引。div被设置为具有底部边界和向左浮动的块元素。它们最终看起来像表格单元格。这有点做作,但它有效,并得到了所需的最终结果。