本文以實例形式展現了C#實現動態生成表格的方法,分享給大家供大家參考之用。具體方法如下:
public string CreateTable(){ StringBuilder sb = new StringBuilder(""); int row = 1;//行數 if (true )//是否有數據 { int nRowCount = 10;//所有條數 row = (int)Math.Ceiling(nRowCount / 5.0);//5.0表示每行有多少條數據 int colNum = 5;//列數 for (int m = 0; m < row; m++) { //if (m % 2 == 0) //偶數行 //{ // sb.Append("<tr class=/"one_tr/">"); //偶數行樣式 //} //else //奇數行 //{ // sb.Append("<tr class=/"two_tr/">"); //奇數行樣式 //} sb.Append("<tr>"); //加行數 for (int n = 0; n < colNum; n++) { sb.Append("<td>"); int currentCount = m * 5 + n;//當前所處條數 if (currentCount < nRowCount)//當前所處條數是否在數據量的有效范圍內 { //添加表格內的內容 } else { //置空 sb.Append(" "); } sb.Append("</td>"); } sb.Append("</tr>"); } } return sb.ToString();}
希望本文所述對大家的C#程序設計有所幫助
新聞熱點
疑難解答