17,740
社区成员
发帖
与我相关
我的任务
分享
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = CreateHtml();
}
private class DataInfo
{
public int ServerID { get; set; }
public DateTime Date { get; set; }
public int Price { get; set; }
public int Users { get; set; }
public int ARpu { get; set; }
}
public class ServerInfo
{
public int ServerID { get; set; }
public string ServerName { get; set; }
}
private string CreateHtml()
{
List<ServerInfo> servers = new List<ServerInfo>();
servers.Add(new ServerInfo() { ServerID = 1, ServerName = "服务器1" });
servers.Add(new ServerInfo() { ServerID = 2, ServerName = "服务器2" });
servers.Add(new ServerInfo() { ServerID = 3, ServerName = "服务器3" });
List<DataInfo> datas = new List<DataInfo>();
datas.Add(new DataInfo() { ServerID = 1, ARpu = 100, Date = DateTime.Parse("2013-1-2"), Price = 1500, Users = 1100 });
datas.Add(new DataInfo() { ServerID = 1, ARpu = 190, Date = DateTime.Parse("2013-1-3"), Price = 1600, Users = 1230 });
datas.Add(new DataInfo() { ServerID = 1, ARpu = 187, Date = DateTime.Parse("2013-1-4"), Price = 1200, Users = 180 });
datas.Add(new DataInfo() { ServerID = 1, ARpu = 120, Date = DateTime.Parse("2013-1-5"), Price = 1300, Users = 176 });
datas.Add(new DataInfo() { ServerID = 2, ARpu = 200, Date = DateTime.Parse("2013-1-2"), Price = 2500, Users = 2100 });
datas.Add(new DataInfo() { ServerID = 2, ARpu = 290, Date = DateTime.Parse("2013-1-3"), Price = 2600, Users = 2230 });
datas.Add(new DataInfo() { ServerID = 2, ARpu = 287, Date = DateTime.Parse("2013-1-4"), Price = 2200, Users = 280 });
datas.Add(new DataInfo() { ServerID = 2, ARpu = 220, Date = DateTime.Parse("2013-1-5"), Price = 2300, Users = 276 });
datas.Add(new DataInfo() { ServerID = 3, ARpu = 300, Date = DateTime.Parse("2013-1-2"), Price = 3500, Users = 3100 });
datas.Add(new DataInfo() { ServerID = 3, ARpu = 390, Date = DateTime.Parse("2013-1-3"), Price = 3600, Users = 3230 });
datas.Add(new DataInfo() { ServerID = 3, ARpu = 387, Date = DateTime.Parse("2013-1-4"), Price = 3200, Users = 380 });
datas.Add(new DataInfo() { ServerID = 3, ARpu = 320, Date = DateTime.Parse("2013-1-5"), Price = 3300, Users = 376 });
string header = string.Empty;
string columns = string.Empty;
string DataRows = string.Empty;
for (int i = 0; i < servers.Count; i++)
{
header += ServerHeader(servers[i]); //分组
columns += Columns(); //每一组三字段
}
//找出记录中日期
List<DateTime> timeS = datas.GroupBy(o => o.Date).Select(o => o.Key).ToList();
//行
foreach (DateTime time in timeS)
{
DataRows += DataRow(time, servers, datas);
}
string table = @"<table style='width: 100%' class='style1' border='1'>
<tr>
<td rowspan='2' class='style2'>日期</td>
<!--每个服务器-->
{Server}
</tr>
<tr>
<!--每个服务器三项-->
{Columns}
</tr>
{DataRows}
</table>";
table = table.Replace("{Server}", header);
table = table.Replace("{Columns}", columns);
table = table.Replace("{DataRows}", DataRows);
return table;
}
private string ServerHeader(ServerInfo info)
{
return "<td colspan='3' class='style2'>" + info.ServerName + "</td>";
}
private string Columns()
{
return @"<td class='style2'>金额</td>
<td class='style2'>人数</td>
<td class='style2'>ACPU</td>";
}
private string DataRow(DateTime date,List<ServerInfo> servers,List<DataInfo> datas)
{
string row=@"<tr>
<td class='style2'>"+date.ToString("yyyy-MM-dd")+"</td>";
List<DataInfo> dataLit = datas.FindAll(o => o.Date == date);
foreach (ServerInfo item in servers)
{
foreach (DataInfo data in dataLit.FindAll(o => o.ServerID == item.ServerID))
{
row += "<td class='style2'>" + data.Price + "</td>";
row += "<td class='style2'>" + data.Users + "</td>";
row += "<td class='style2'>" + data.ARpu + "</td>";
}
}
row += "</tr>";
return row;
}
}
如图:
没做统计,你自己加统计吧调用方法是一样的相当于加个服务器。下边的就多加一行
<table id="table1" border="1" >
<tr>
<th rowspan="2">日期</th>
<asp:Repeater ID="servers" runat="server" OnItemDataBound="servers_OnItemDataBound">
<ItemTemplate>
<th colspan="3">
<%#Container.DataItem%>
<table border="1">
<tr>
<th>金额</th>
<th>人数</th>
<th>ARPU</th>
</tr>
</table>
</th>
<asp:Label ID="ld" runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
<th colspan="3">
总计
<table border="1">
<tr>
<th>金额</th>
<th>人数</th>
<th>ARPU</th>
</tr>
</table>
</th>
</tr>
</table>
我这样写,能实现表头的绑定了,想分别去表头绑定的服务器再实现一次绑定。于是问题转化为,如何得到各个服务器,内嵌个ListView再绑定数据,有没什么想法呢?[/quote]
绑定的方式好像无解。。。。[/quote]
哎,我也觉得!擦,自己摸索,效率好低啊。。。你说的第三方控件叫什么名?想得出的话就告诉我下,谢谢呀![/quote]
DevExpress 这个里面有DEMO你自己看吧。不过还建议用Response.Write方式输出Html更容易些。[/quote]
用你说的这种方式,能很好实现数据绑定么?能否给个简单例子我看看?[/quote]
我的的方式就是让你抛弃绑定方式,用循环的方式,你非得要绑定那就用DevExpress应是最好的了。[/quote]
恩,我不是非要用绑定,只要有方法实现就行!用你这个方法,我不知怎么获取表头的值。我理解的你的思路是用html建个表,但是这个表的第几行第几列的数值,我能在后台怎么获取,能否调用后头函数对表进行创建的?这些都不知道呀!!![/quote]
这当然可以做的到呀,因为你的表头肯定是从记录中取得的吧,如果你是绑定的那就只能从字段名中取到,[/quote]
恩的,记录能获得,不是问题。问题是我要用后台C#代码建前台那个表,我还不知怎么写。然后Response.Write这个是能把数据输到前台的指定位置的吧?[/quote]
这当然可以[/quote]
能给个简单例子吗?你写个简单的,解决这两个问题,估计就差分页了。[/quote]
你先放数据上来看格式是啥样,晚上回去给你写个DEMO。[/quote]
好的,稍等。[/quote]
哥,留个Q行不?我发Q上,或者我的:747508008[/quote]
不让上Q呀。还是发论坛。[/quote]
哦,好吧![/quote]
这个回复有点长哈,我发在另个回复上了。麻烦你跑一趟啦,先谢谢啦!感激啊!!