2014-03-02 44 views
0

我想從上面的數據庫查詢填充一個列表框。列表框顯示正確的「PLAYER」結果作爲items.I還要爲所有這些項目分配一個值成員作爲數據庫的「ID」。它可以是這樣嗎?通過列表框中顯示的項目也傳遞值Winforms c#

SqlDataAdapter sdaa = new SqlDataAdapter("SELECT PLAYERS.ID, CAST(PLAYERS.NO AS VARCHAR) + ', ' + PLAYERS.FIRSTNAME AS PLAYER FROM PLAYERS INNER JOIN TEAMS ON PLAYERS.TEAM_ID = TEAMS.ID WHERE (TEAMS.NAME =oly ", con); 
     DataTable dta = new DataTable(); 
     sdaa.Fill(dta); 

      for (int i = 0; i < dt.Rows.Count; i++) 
     { 

      listBox1.Items.Add(dt.Rows[i]["PLAYER"].ToString()); 
     } 

回答

2

您必須將dataTablelistbox.DataSource,然後設置ValueMemeber(隱藏字段)和顯示部件

SqlDataAdapter sdaa = new SqlDataAdapter("SELECT PLAYERS.ID, CAST(PLAYERS.NO AS VARCHAR) + ', ' + PLAYERS.FIRSTNAME AS PLAYER FROM PLAYERS INNER JOIN TEAMS ON PLAYERS.TEAM_ID = TEAMS.ID WHERE (TEAMS.NAME =oly ", con); 
    DataTable dta = new DataTable(); 
    sdaa.Fill(dta); 
    listbox3.DataSource=dta; 
    listbox3.DisplayMember="PLAYER"; 
    listbox3.ValueMember="ID"; 
+0

怎麼可以這樣?用代碼? – Apollon

+0

對不起,我不明白你想要什麼。你想手動添加值(使用循環)價值memebers? @Apollon – Tinwor

相關問題