关于.net 数据绑定的一个问题,救救深夜仍在奋斗的猿吧!!!
划红线跟空白部分要数据绑定,这种格局需要什么控件?用Repeater怎么架构这种布局呢?
夜深了,苦逼的程序员还在奋斗,希望有知人士,给点想法。。。更希望的是,能给个相关的模板代码。
谢谢啦~~~
[解决办法]
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;
}
}