2016-05-07 57 views
0

enter image description here如何在GridView中獲取列以顯示與其ID相對應的名稱?

我有一個GridView連接到SQL數據源,我想顯示爲主客場球隊名稱。相反,我只能檢索他們的Home_Team_IDAway_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團隊表。我該如何解決? enter image description here

<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。

+0

因此,而不是顯示'8',你想顯示'Dodgers'什麼的? – gmiley

+0

是的!主隊和客隊的ID與Team_ID匹配,因爲他們是外鍵。 –

+0

你的SQL查詢看起來像用來生成數據的東西嗎?另外,你能提供一個只加載到網格中的數據的樣本嗎?假設數據是正確的,你應該能夠以與ID綁定相同的方式綁定包含團隊名稱的列。 – gmiley

回答

0

我相信你會想要設置HeaderText的列。

foreach(var col in EnterMatchGridView.Columns) 
{ 
    col.HeaderText = "Some Text"; 
} 

上面的例子並不完全是你想要的,但它確實顯示瞭如何訪問它。如果您在ASP代碼中查看以上內容,您將看到<asp:TemplateField HeaderText="Away_team_ID" ...>,如果您不需要通過代碼進行設置,那麼您可以在其中修改它。

如果你想要的話,你也可以綁定一些屬性。

澄清後,您應該確保您的SQL查詢正確選擇數據。不知道你的表結構,我只能猜測,但你可能要像:

select t1.home_team_id, t2.team_name as home_team_name, 
     t1.away_team_id, t3.team_name as away_team_name, 
     t1.home_team_score, t1.away_team_score, t1.game_date 
from game_table t1 
join team_table t2 
on t2.team_id = t1.home_team_id 
join team_table t3 
on t3.team_id = t1.away_team_id; 

然後你就可以在你的列home_team_nameaway_team_name綁定。

使用數據表,如上文所述,您應該能夠做到以下幾點,然後綁定到results爲您的數據表:

DataTable games = new DataTable() 
games.Columns.Add("Home_Team_ID", typeof(int)); 
games.Columns.Add("Home_Team_Name", typeof(string)); 
games.Columns.Add("Away_Team_ID", typeof(int)); 
games.Columns.Add("Away_Team_Name", typeof(string)); 
games.Columns.Add("Home_Team_Score", typeof(int)); 
games.Columns.Add("Away_Team_Score", typeof(int)); 
games.Columns.Add("Game_Date", typeof(DateTime)); 

var results = from dataRows1 in MatchStatistics.AsEnumerable() 
       join dataRows2 in Teams.AsEnumerable() 
       on dataRows1.Field<int>("Home_Team_ID") equals dataRows2.Field<int>("Team_ID") 
       join dataRows3 in Teams.AsEnumerable() 
       on dataRows1.Field<int>("Away_Team_ID") equals dataRows3.Field<int("Team_ID") 
       select games.LoadDataRow(new object[] 
       { 
        dataRows1.Field<int>("Home_Team_ID"), 
        dataRows2.Field<string>("Team_Name"), 
        dataRows1.Field<int>("Away_Team_ID"), 
        dataRows3.Field<string>("Team_Name"), 
        dataRows1.Field<int>("Home_Team_Score"), 
        dataRows1.Field<int>("Away_Team_Score"), 
        dataRows1.Field<DateTime>("Game_Date") 
       }, false); 

歸根結底,它歸結爲是,你需要訪問Teams使用該關係的DataTable,除非通過Team_ID篩選結果,否則不能只訪問表格,否則您只會獲取第一個項目或DataTable中的所有項目。

+0

頭文件不是問題,我只需要ID就可以顯示相應的名稱 –

+0

您可以編輯您的問題以提供您希望網格看起來像什麼的示例?這是不是很清楚你想要什麼。試着提供一個你希望網格看起來如何以及它看起來如何的simpel例子,即'Column1 | Column2 | Column3' '======================''value1 | value2 | value3' - 等等,但顯然格式正確。只有這樣我們才能瞭解您的期望。 – gmiley

相關問題