4

例子:

<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();   

当我运行代码报告列显示 System.Collections.Generic 而不是字符串值

Name    Frequency       Reports
A           Daily         System.Collections.Generic.List`1[System.String] 
B           Weekly        System.Collections.Generic.List`1[System.String] 

我需要这样的输出:报告列应该显示字符串列表

Name    Frequency   Reports
A            Daily            1.XYZ
                          2.ABC
                          3.PQR
B           Weekly            1.XYZ
4

4 回答 4

4

然后,您需要指定 Reports 列的数据类型:

        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>
于 2013-06-26T09:16:08.743 回答
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);     
于 2013-06-26T09:01:45.763 回答
0

GridView 无法自动确定如何呈现集合。

您可以将另一个数据绑定控件(ListView 或 GridView)放在 Reports 列中,然后将其绑定到 RowDataBound 等事件的报告列表中。

参考: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>
于 2013-06-26T09:05:31.720 回答
0

您正在尝试在列中显示对象。TemplateColumn 中的标签不知道如何显示字符串值列表。您应该在标签中放置一个字符串,如下所示:

int i = 2;
var res = "1. " + report1.Aggregate((c, n) => c + String.Format("\r\n{0}. {1}", i++, n));
row1["Reports"] = res;

这将给出预期的结果。

于 2013-06-26T09:02:52.297 回答