簡體   English   中英

下拉列表添加列表項

[英]Dropdownlist adding list items

我已經將本地數據庫中的一些數據添加到List中,現在我需要在下拉列表中顯示此List所具有的值。 該列表包含以下數據:

  • Java的
  • 。凈
  • JavaScript的
  • 紅寶石
  • 蟒蛇

方法:

public List<string> DisplayTopicNames()
{


    // declare the connection string 
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True";

    // Initialise the connection 
    OleDbConnection myConn = new OleDbConnection(database);
    //Query
    string queryStr = "SELECT TopicName FROM Topics";
    // Create a command object 
    OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
    // Open the connection 
    myCommand.Connection.Open();
    // Execute the command 
    OleDbDataReader myDataReader = myCommand.ExecuteReader();


    // Extract the results 
   List<string> topicNames = new List<string>();

    while (myDataReader.Read())
    {
        topicNames.Add(myDataReader.GetValue(0).ToString());
    }

    // close the connection 
    myCommand.Connection.Close();
    return topicNames;
}

}

當我自己返回上面的方法時它工作正常並且所有的值都在那里但是當我使用下面的代碼將這個List添加到下拉列表時它只顯示“j”“a”“v”“a”這是非常的奇怪。

DropDownList1.DataSource = dt.DisplayTopicNames(); 
DropDownList1.DataBind();

代替:

topicNames.Add(myDataReader.GetValue(0).ToString());

嘗試這個:

topicNames.Add(myDataReader["TopicName"].ToString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM