1

我有一个由对象类填充的消息网格视图。对象中有一个列表,因此对于每条消息,都有一个收件人列表。问题是,现在 gridview 只为每条消息显示“Person []”。我希望它显示为列表中收件人的计数。

任何帮助,将不胜感激。

HTML

    <asp:GridView
        ID="grdMsgSent"
        runat="server"
        CssClass="cellSpacing"
        OnRowDataBound="grdMsgSent_RowDataBound"
        AllowSorting="True"
        EmptyDataText="You have not sent any messages."
        AllowPaging="True"
        PageSize="6"
        OnPageIndexChanging="grdMsgSent_PageIndexChanging"
        OnSorting="grdMsgSent_Sorting"
        AutoGenerateColumns="False">
<Columns>

<asp:BoundField DataField="Recipients" HeaderText="Recipients" />
</Columns>
</asp:GridView>

C#

grdMsgSent.DataSource = listSentMsg.List;
            grdMsgSent.DataBind();
4

2 回答 2

1

For example, if you have a data column name called UserID, you can populate in grid view using Eval (DataBinding Expression). So your code will be like this in aspx page,

<asp:TemplateField HeaderText="UserID" Visible="false">
                                    <ItemTemplate>
                                        <asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>

In C# Code Behind,

DataTable dataTable = new DataTable();
        DataColumn dataColumn;
        dataColumn = new DataColumn("UserID");
于 2013-09-26T01:34:56.990 回答
0

您可以使用模板字段:

对于数组 [],请执行以下操作:

<Columns>

    <asp:TemplateField>
        <ItemTemplate>                        
            <asp:Label ID="Label1"  runat="server" Text='<%#Eval("Recipients.Length") %>' />
        </ItemTemplate>
    </asp:TemplateField>

对于列表,请执行以下操作:

<Columns>

    <asp:TemplateField>
        <ItemTemplate>                        
            <asp:Label ID="Label1"  runat="server" Text='<%#Eval("Recipients.Count") %>' />
        </ItemTemplate>
    </asp:TemplateField>
于 2013-09-26T01:22:31.170 回答