示例:
<asp:GridView ID="reportScheduleDetailsGridView" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ></asp:BoundField>
<asp:BoundField DataField="Frequency" HeaderText="Frequency" ></asp:BoundField>
<asp:TemplateField HeaderText="Reports" >
<ItemTemplate>
<asp:Label ID="cblSubscribedReports" Text='<%# Eval("Reports") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码:
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Frequency");
table.Columns.Add("Reports");
DataRow row1 = table.NewRow();
row1["Name"] = "A";
row1["Frequency"] = "Daily";
List<string> report1 = new List<string>();
report1.Add("XYZ");
report1.Add("ABC");
report1.Add("PQR");
row1["Reports"] = report1;
table.Rows.Add(row1);
DataRow row2 = table.NewRow();
row2["Name"] = "B";
row2["Frequency"] = "Weekly";
List<string> report2 = new List<string>();
report2.Add("XYZ");
row2["Reports"] = report2;
table.Rows.Add(row2);
reportScheduleDetailsGridView.DataSource = table;
reportScheduleDetailsGridView.DataBind();
当我运行代码时,Reports列显示System.Collections.Generic而不是字符串值
Name Frequency Reports
A Daily System.Collections.Generic.List`1[System.String]
B Weekly System.Collections.Generic.List`1[System.String]
我需要这样的输出:Report列应该显示字符串
的列表Name Frequency Reports
A Daily 1.XYZ
2.ABC
3.PQR
B Weekly 1.XYZ
答案 0 :(得分:2)
然后您指定报告列的数据类型:
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Frequency");
table.Columns.Add("Reports", typeof(List<string>));
DataRow row1 = table.NewRow();
row1["Name"] = "A";
row1["Frequency"] = "Daily";
List<string> report1 = new List<string>();
report1.Add("XYZ");
report1.Add("ABC");
report1.Add("PQR");
row1["Reports"] = report1;
table.Rows.Add(row1);
DataRow row2 = table.NewRow();
row2["Name"] = "B";
row2["Frequency"] = "Weekly";
List<string> report2 = new List<string>();
report2.Add("XYZ");
row2["Reports"] = report2;
table.Rows.Add(row2);
reportScheduleDetailsGridView.DataSource = table;
reportScheduleDetailsGridView.DataBind();
<asp:GridView ID="reportScheduleDetailsGridView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
<asp:BoundField DataField="Frequency" HeaderText="Frequency"></asp:BoundField>
<asp:TemplateField HeaderText="Reports">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("Reports") %>'>
<ItemTemplate>
<%# (Container.ItemIndex+1)+"."+ Container.DataItem %><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 1 :(得分:1)
你可以像这样显示逗号分隔值
DataRow row2 = table.NewRow();
row2["Name"] = "B";
row2["Frequency"] = "Weekly";
List<string> report2 = new List<string>();
report2.Add("XYZ");
string strReports = string.Join(",", report2.ToArray());
row2["Reports"] = strReports ;
table.Rows.Add(row2);
答案 2 :(得分:0)
您正尝试在列中显示该对象。 TemplateColumn中的Label不知道如何显示字符串值列表。您应该在Label中放置一个字符串,如下所示:
int i = 2;
var res = "1. " + report1.Aggregate((c, n) => c + String.Format("\r\n{0}. {1}", i++, n));
row1["Reports"] = res;
这将给出预期的结果。
答案 3 :(得分:0)
GridView无法自动弄清楚如何呈现集合。
您可以在Reports列中放入另一个数据绑定控件(ListView或GridView),然后将其绑定到RowDataBound等事件的报表列表。
参考:Hierarchical Data Binding in ASP.NET
<ItemTemplate>
<asp:Repeater Runat="server" EnableViewState="false"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Reports")%>'>
<ItemTemplate>
<br/><asp:Label ID="cblSubscribedReports"
Text='<%# DataBinder.Eval(Container.DataItem, "Report") %>'/>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>