我有一個GridView連接到SQL數據源,我想顯示爲主客場球隊名稱。相反,我只能檢索他們的Home_Team_ID
和Away_Team_ID
,因爲這些匹配他們雙方的Team_ID that is stored in another table. I've tried "Bind("Team_name")" but this returns the first team name in the
團隊表。我該如何解決?
<asp:GridView ID="EnterMatchGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="Team_ID" DataSourceID="SqlDataSource2" OnRowDataBound="EnterMatchGridView_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Home_team_ID" SortExpression="Home_team_ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_team_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Home_team_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Away_team_ID" SortExpression="Away_team_ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Away_team_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Away_team_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Home_team_score" HeaderText="Home_team_score" SortExpression="Home_team_score" />
<asp:BoundField DataField="Away_team_score" HeaderText="Away_team_score" SortExpression="Away_team_score" />
<asp:TemplateField HeaderText="Game_date" SortExpression="Game_date">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Game_date") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Game_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SportsData2ConnectionString %>" SelectCommand="SELECT MatchStatistics.Home_team_ID, MatchStatistics.Away_team_ID, MatchStatistics.Home_team_score, MatchStatistics.Away_team_score, MatchStatistics.Game_date, Team.Team_ID, Team.Team_name FROM MatchStatistics INNER JOIN Team ON (MatchStatistics.Home_team_ID = Team.Team_ID OR MatchStatistics.Away_team_ID = Team.Team_ID) AND MatchStatistics.Home_team_ID = Team.Team_ID WHERE (Team.Team_ID = @Team_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="HomeFormDDL" Name="Team_ID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
如果每個家庭和客隊柱說,他們的ID(在這種情況下,8和32)我想與其對應的Team_IDs
例如更換這些阿斯頓維拉和諾維奇分別有Team_ID
8和32。
因此,而不是顯示'8',你想顯示'Dodgers'什麼的? – gmiley
是的!主隊和客隊的ID與Team_ID匹配,因爲他們是外鍵。 –
你的SQL查詢看起來像用來生成數據的東西嗎?另外,你能提供一個只加載到網格中的數據的樣本嗎?假設數據是正確的,你應該能夠以與ID綁定相同的方式綁定包含團隊名稱的列。 – gmiley