小弟写一个用户自定义控件,就是显示数据库中的信息,是显示两列信息内容,第一列显示5条信息后要换行到第二列继续显示,小弟想不明白应该怎么去循环,这个控件每行是一个 <tr> 每列是 <tr> 中的 <td> 我把我写的代码粘到下边
if(dt!=null)
{
Merchandise_Seek += " <table width= '100% '> ";
for(int i=0;i <dt.Rows.Count;i++)
{
if(i <Info_Amount)
{
Merchandise_Seek += " <tr> ";
Merchandise_Seek += " <td align= 'left ' width=50%> <span class= 'span4 '> <a class= 'span4 ' href= ' "+dt.Rows[i][ "id "].ToString()+ " '> "+Return_sortstring(dt.Rows[i][ "name "].ToString(),InfoShow_Amount)+ " </a> </span> </td> ";
Merchandise_Seek += " <td align= 'left ' width=50%> <span class= 'span4 '> <a class= 'span4 ' href= ' "+dt.Rows[i][ "id "].ToString()+ " '> "+Return_sortstring(dt.Rows[i][ "name "].ToString(),InfoShow_Amount)+ " </a> </span> </td> ";
Merchandise_Seek += " </tr> ";
}
}
}
现在这样循环出来后两个 <td> 中显示的是一样的数据,请问我应该怎么循环或者判断才能达到第一个 <td> 中显示5条数据后在第二个 <td> 中继续显示数据呢?拜托高手,谢谢了
------解决方案--------------------------------------------------------
呵呵,建議樓主用DataList控件實現,省事多了!
------解决方案--------------------------------------------------------
for(int i=0;i <dt.Rows.Count;i++){ if(i % 5 == 0) { Merchandise_Seek += " <tr> "; } Merchandise_Seek += " <td align= 'left ' width=50%> <span class= 'span4 '> <a class= 'span4 ' href= ' "+dt.Rows[i][ "id "].ToString()+ " '> "+Return_sortstring(dt.Rows[i][ "name "].ToString(),InfoShow_Amount)+ " </a> </span> </td> "; Merchandise_Seek += " <td align= 'left ' width=50%> <span class= 'span4 '> <a class= 'span4 ' href= ' "+dt.Rows[i][ "id "].ToString()+ " '> "+Return_sortstring(dt.Rows[i][ "name "].ToString(),InfoShow_Amount)+ " </a> </span> </td> "; if( (i+1) % 5 == 0) { Merchandise_Seek += " </tr> "; }}if(dt.Rows.Count % 5 != 0){ Merchandise_Seek += " </tr> ";}
------解决方案--------------------------------------------------------
string Merchandise_Seek = " ";
DataTable dt = new DataTable();
if (dt != null)
{
Merchandise_Seek += " <table width= '100% '> ";
Merchandise_Seek += " <tr> ";
for (int i = 0; i < dt.Rows.Count; i += 5)
{
Merchandise_Seek += " <td> ";
for (int j = 0; j < 5; j++)
{
Merchandise_Seek += " <table width= '100% '> ";
Merchandise_Seek += " <tr> ";
Merchandise_Seek += " <td> ";
Merchandise_Seek += dt.Rows[i + j][0].ToString();
Merchandise_Seek += " </td> ";
Merchandise_Seek += " </tr> ";
Merchandise_Seek += " </table> ";