0

我有一个 asp.net 项目和 2 个表:

报告

RegisterID EventID UserID DateTime

用户

用户 ID 用户名

现在我想要一个显示日期时间和用户名的网格视图。

我从 2 小时开始尝试这个,但没有一次机会展示这个。

怎么做?

直到现在我有这个:

        public static List<EventRegistration> GetAllRegistrationsForEventID(Guid eventID)
    {
        using (CyberDBDataContext db = new CyberDBDataContext())
        {
            DataLoadOptions options = new DataLoadOptions();

            options.LoadWith<EventRegistration>(p => p.CyberUser);
            db.LoadOptions = options;

            List<EventRegistration> list = (from a in db.EventRegistrations where a.EventID == eventID select a).ToList();
            return list;
        }

和这个:

             <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllRegistrationsForEventID"
        TypeName="DAL.RegistrationHandler">
        <SelectParameters>
            <asp:QueryStringParameter DbType="Guid" DefaultValue="0000-000000-000000-0000" Name="eventID"
                QueryStringField="id" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:GridView  ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"
        EmptyDataText="Keine Spieler registriert" 
        onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="RegisterID" HeaderText="RegisterID" SortExpression="RegisterID"
                Visible="false" />
            <asp:BoundField DataField="EventID" HeaderText="EventID" SortExpression="EventID"
                Visible="false" />
            <asp:TemplateField HeaderText='Name' ItemStyle-HorizontalAlign='Center'>
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
                </ItemTemplate>

            </asp:TemplateField>
            <asp:BoundField DataField="Timestamp" HeaderText="Anmeldezeit" SortExpression="Timestamp" />
            <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID"  />
        </Columns>
    </asp:GridView>

但没有任何效果

4

1 回答 1

0

您的 select 方法是GetAllRegistrationsForEventID返回 EventRegistration 列表的方法。

返回一个将事件和用户信息投影在一起的匿名对象,例如: 首先有一个类:

class AllInfo
{
    string RegisterID ;
    string EventID ;
    string UserID ;
    string DateTime;
    string Username;
}

然后

public static List<AllInfo> yourMethodName(Guid eventID)
{
    using (CyberDBDataContext db = new CyberDBDataContext())
    {
        DataLoadOptions options = new DataLoadOptions();

        options.LoadWith<EventRegistration>(p => p.CyberUser);
        db.LoadOptions = options;

        List<AllInfo> list = (from a in db.EventRegistrations 
                             where a.EventID == eventID 
                             select new AllInfo (){ RegisterID = a.RegisterID, 
                                                    EventID = a.EventID,
                                                    UserID = a.UserID,
                                                    UserName = a.User.UserName...}).ToList();
        return list;
    }

}

于 2012-12-07T15:49:46.260 回答