使用2个表中的数据填充GridView

时间:2012-12-07 15:08:09

标签: c# asp.net gridview

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

Regitrations

RegisterID 事件ID 用户身份 日期时间

用户

用户名 用户名

现在我想要一个显示DateTime和Username的网格视图。

我尝试了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>

但没有任何作用

1 个答案:

答案 0 :(得分: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;
    }

}