C# 用2个表中的数据填充GridView
我有一个asp.net项目和两个表: 地区 注册ID 事件 用户ID 日期时间 使用者 用户ID 用户名 现在我想要一个显示日期时间和用户名的gridview 我试了两个小时,但没有一次机会展示这个 怎么办 到目前为止,我有以下几点:C# 用2个表中的数据填充GridView,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个asp.net项目和两个表: 地区 注册ID 事件 用户ID 日期时间 使用者 用户ID 用户名 现在我想要一个显示日期时间和用户名的gridview 我试了两个小时,但没有一次机会展示这个 怎么办 到目前为止,我有以下几点: public static List<EventRegistration> GetAllRegistrationsForEventID(Guid eventID) { using (CyberDBDataCont
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;
}
公共静态列表GetAllRegistrationForeventid(Guid eventID)
{
使用(CyberDBDataContext db=new CyberDBDataContext())
{
DataLoadOptions=new DataLoadOptions();
options.LoadWith(p=>p.CyberUser);
db.LoadOptions=选项;
List List=(从db.EventRegistrations中的a开始,其中a.EventID==EventID选择a).ToList();
退货清单;
}
这是:
<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>
但是什么都不起作用您选择的方法是
GetAllRegistrationForeventid
,它返回事件注册列表
返回将事件和用户信息投影在一起的匿名对象,如:
第一节课:
class AllInfo
{
string RegisterID ;
string EventID ;
string UserID ;
string DateTime;
string Username;
}
然后
公共静态列表yourMethodName(Guid事件ID)
{
使用(CyberDBDataContext db=new CyberDBDataContext())
{
DataLoadOptions=new DataLoadOptions();
options.LoadWith(p=>p.CyberUser);
db.LoadOptions=选项;
List List=(从db.EventRegistrations中的
其中a.EventID==EventID
选择new AllInfo(){RegisterID=a.RegisterID,
EventID=a.EventID,
UserID=a.UserID,
UserName=a.User.UserName…}).ToList();
退货清单;
}
}
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;
}