如何动态实现纵横向合并table Refer this:合纵连横(GridView或Repeater)控件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个纵横一起合并,咋弄呢。我自己写了个拼接字符串的横向合并方法,感觉效率不高string TableHtml(DataTable dt) { System.Text.StringBuilder html = new System.Text.StringBuilder(); foreach (DataRow row in dt.Rows) { html.Append("<tr>"); //重复次数 int repeatTimes = 1; //重复字段 string repeatString = string.Empty; for (int i = 0;i < row.ItemArray.Length; i++) { if (repeatString == string.Empty) { if (row.ItemArray[i].ToString() == string.Empty) repeatTimes++; repeatString = row.ItemArray[i].ToString(); continue; } if (row.ItemArray[i].ToString() == repeatString) { repeatTimes++; if (i == row.ItemArray.Length - 1) { html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString)); repeatTimes = 1; repeatString = string.Empty; } } else { if (repeatTimes > 1) { if (i == row.ItemArray.Length-1) { html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes+1, repeatString)); repeatTimes = 1; repeatString = string.Empty; continue; } html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString)); repeatTimes = 1; } else { if (i == row.ItemArray.Length-1) { html.Append(string.Format("<td>{0}</td>", repeatString)); html.Append(string.Format("<td>{0}</td>", row.ItemArray[i].ToString())); repeatString = string.Empty; continue; } html.Append(string.Format("<td>{0}</td>", repeatString)); } repeatString = row.ItemArray[i].ToString(); } } html.Append("</tr>"); } return html.ToString(); } 发现一个bug,当字段为NULL或者String.Empty 的时候会出问题,自己画蛇添足了string TableHtml(DataTable dt) { System.Text.StringBuilder html = new System.Text.StringBuilder(); foreach (DataRow row in dt.Rows) { html.Append("<tr>"); //重复次数 int repeatTimes = 1; //重复字段 string repeatString = string.Empty; for (int i = 0;i < row.ItemArray.Length; i++) { string NowString = row.ItemArray[i] == null ? string.Empty : row.ItemArray[i].ToString(); if (NowString == repeatString) { repeatTimes++; if (i == row.ItemArray.Length - 1) { html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString)); repeatTimes = 1; repeatString = string.Empty; } } else { if (repeatTimes > 1) { if (i == row.ItemArray.Length-1) { html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes+1, repeatString)); repeatTimes = 1; repeatString = string.Empty; continue; } html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString)); repeatTimes = 1; } else { if (i == row.ItemArray.Length-1) { html.Append(string.Format("<td>{0}</td>", repeatString)); html.Append(string.Format("<td>{0}</td>", NowString)); repeatString = string.Empty; continue; } html.Append(string.Format("<td>{0}</td>", repeatString)); } repeatString = NowString; } } html.Append("</tr>"); } return html.ToString(); } jquery 关于asp.net弹出对话框的问题 GridView数据绑定后 显示别名.... 无效操作。连接被关闭 如何做亚马逊代购网站 关于Visual Studio2005开发ASP.NET网站的问题 关于DVBBS2005的难题,不知道谁看过DVBBS2005的代码. 如何讲aspx文件转换成asp文件 (在线等) Web的安装程序,如何不让用户去选择虚拟目录? 各位大虾:我如何将datagrid中的数据填到水晶报表中?在线,急急!! GridView中如何实现上移多行和下移多行,其中上移和下移已经实现,求高手! 请问如何快速的摆脱当前的现状??????
string TableHtml(DataTable dt)
{
System.Text.StringBuilder html = new System.Text.StringBuilder();
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
//重复次数
int repeatTimes = 1;
//重复字段
string repeatString = string.Empty;
for (int i = 0;i < row.ItemArray.Length; i++)
{
if (repeatString == string.Empty)
{
if (row.ItemArray[i].ToString() == string.Empty)
repeatTimes++;
repeatString = row.ItemArray[i].ToString();
continue;
}
if (row.ItemArray[i].ToString() == repeatString)
{
repeatTimes++;
if (i == row.ItemArray.Length - 1)
{
html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
repeatTimes = 1;
repeatString = string.Empty;
}
}
else
{
if (repeatTimes > 1)
{
if (i == row.ItemArray.Length-1)
{
html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes+1, repeatString));
repeatTimes = 1;
repeatString = string.Empty;
continue;
}
html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
repeatTimes = 1;
}
else
{
if (i == row.ItemArray.Length-1)
{
html.Append(string.Format("<td>{0}</td>", repeatString));
html.Append(string.Format("<td>{0}</td>", row.ItemArray[i].ToString()));
repeatString = string.Empty;
continue;
}
html.Append(string.Format("<td>{0}</td>", repeatString));
}
repeatString = row.ItemArray[i].ToString();
}
}
html.Append("</tr>");
}
return html.ToString();
}
{
System.Text.StringBuilder html = new System.Text.StringBuilder();
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
//重复次数
int repeatTimes = 1;
//重复字段
string repeatString = string.Empty;
for (int i = 0;i < row.ItemArray.Length; i++)
{
string NowString = row.ItemArray[i] == null ? string.Empty : row.ItemArray[i].ToString();
if (NowString == repeatString)
{
repeatTimes++;
if (i == row.ItemArray.Length - 1)
{
html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
repeatTimes = 1;
repeatString = string.Empty;
}
}
else
{
if (repeatTimes > 1)
{
if (i == row.ItemArray.Length-1)
{
html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes+1, repeatString));
repeatTimes = 1;
repeatString = string.Empty;
continue;
}
html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
repeatTimes = 1;
}
else
{
if (i == row.ItemArray.Length-1)
{
html.Append(string.Format("<td>{0}</td>", repeatString));
html.Append(string.Format("<td>{0}</td>", NowString));
repeatString = string.Empty;
continue;
}
html.Append(string.Format("<td>{0}</td>", repeatString));
}
repeatString = NowString;
}
}
html.Append("</tr>");
}
return html.ToString();
}