2013-10-07 18 views
1

獲取的DataRow我不知道如何dropdownlist所選項目轉換爲datarow從DropDownList中

sDropDownList.DataSource = GetTable(); 


sDropDownList.DataTextField = "Name";  

sDropDownList.DataValueField = "ID"; 

我試圖從這樣的下拉列表獲得數據行..。

DataRow row = (sDropDownList.SelectedItem as DataRowView).Row; 

但我得到一個錯誤..。錯誤如無法將listitems轉換爲數據行

+0

爲什麼要嘗試將選定的值從DropDownList轉換爲DataRow?真的沒有道理......你究竟想達到什麼目的? – walther

回答

1

回發時不是DataRow。 A DropDownList的產品有TextValue,因爲它是ListItem。如果您稍後需要記錄中的其他信息(在回發中),則必須再次查詢數據庫。

其實你已經擁有了所有你需要的,因爲你已經將DataValueField設置爲ID列。

int id = int.Parse(sDropDownList.SelectedValue); 
// now query the database to get the whole record or the information you need