首先,我要感谢@Aybe 的回复。
我尝试了很多,终于得到了预期的结果。
<div>
<table>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td>
<%#Eval("Key") %>
<div>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:Label runat="server" ForeColor="Red" Text='<%#Eval("MetricName") %>'></asp:Label><br />
<asp:Label runat="server" ForeColor="Red" Text='<%#Eval("FiscalYeartarget") %>'></asp:Label><br />
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
在 C# 中,我创建了一个字典,如下所示:
public Dictionary<string, List<custom>> query1;
string connstr = "Data Source=(local);Initial Catalog=t;Integrated Security=True";
string query = "SELECT * FROM [td]";
DataTable dTable = new DataTable();
using (SqlConnection sqlconn = new SqlConnection(connstr))
{
sqlconn.Open();
SqlDataAdapter sqlda = new SqlDataAdapter(query, sqlconn);
sqlda.Fill(dTable);
query1 = dTable.AsEnumerable().GroupBy(x => x.Field<string>("HeaderName"))
.ToDictionary(grp => grp.Key, x => x.Select(y => new custom()
{
MetricName = y.Field<string>("MetricName/KPI"),
FiscalYeartarget = y.Field<double>("FiscalYeartarget").ToString(),
Status = y.Field<string>("Status"),
VTF = y.Field<double>("VTF").ToString(),
VTF_Percent = y.Field<double>("VTF %").ToString(),
YTDResults = y.Field<double>("YTDResults").ToString(),
YTDTarget = y.Field<double>("YTDTarget").ToString()
}).ToList());
Repeater1.DataSource = query1;
Repeater1.DataBind();
var counter = 0;
foreach (var x in query1)
{
Repeater rptr = (Repeater)Repeater1.Controls[counter].FindControl("Repeater2");
rptr.DataSource = x.Value.ToList();
rptr.DataBind();
counter++;
}
}
最后是我的自定义类:
public class custom
{
public string MetricName { get; set; }
public string FiscalYeartarget { get; set; }
public string YTDTarget { get; set; }
public string YTDResults { get; set; }
public string VTF { get; set; }
public string VTF_Percent { get; set; }
public string Status { get; set; }
}