これは過去数日間私の脳を傷つけていました。それはかなりの難題であることが証明されています。C#/ ASP.NETで配列から垂直方向にソートされたHTMLテーブルを塗りつぶす
私が必要とする最終的な結果は、任意の数の列と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;
あなたは(どこnumStatusesを設定すると、基本的にそれはしかし、作品私はそれをコメントアウトしています)7の倍数(これは最後の列を空白にしてしまいます)または18(これは3行で6列になります)、それは変です。
また、見やすくするためのコードはいくつかありません。私は2D配列を使用して、セルの背景として設定する色を決定しています。
コードが醜い場合はお詫び申し上げます...私は最高のプログラマーではありません。