2011-08-04 23 views
0

我已經完成了將數據從DataSet插入到組合框的代碼序列,但它看起來像是很多工作。我想可能有一個更短的路,然後我想到了這個問題。是否有可能將數據集中表格列中的一組行轉換爲可以使用最少編碼工作量輸入組合框的列表?有沒有辦法將數據集轉換爲可用作組合框數據源的列表?

我嘗試過的方法是使用Dataset.Tables [0] .Select()返回一個dataRowCollection,但是這個想法崩潰並燒燬 - 組合框不想接受它。

另一種方法我試過的工作,但它似乎是一個類似的方法,我與我的長碼:

DataSet comboBoxItems = new SQLCommands().GetStockIDs(); //Calls the uspGetStockIDs Stored procedure,Returning 1 Column (StockID from Stock Table) 
cbStockID.Items.Clear(); //Clears the Combobox 
foreach (DataRow dr in comboBoxItems.Tables[0].Rows) //Then Iterates through the retrieved rows 
{ 
    cbStockID.Items.Add(dr.Field<int>(0)); //and places them in the combobox 
} 

但是上面的代碼並沒有回答我的問題。這是我重新解釋我的問題的地方:是否可以將數據集中表格列中的一組行轉換爲可以使用最少編碼工作輸入到組合框中的列表?

+0

後來檢查答案 - 得跑去購物。 – Eon

回答

1

是否有你不能在你的場景中使用普通數據綁定的原因?這應該工作:

cbStockID.DataSource = comboBoxItems.Tables[0]; 
cbStockID.DisplayMember = "{DisplayTableColumnName}"; //Displayed items in the combo box. 
cbStockID.ValueMember = "{ValueTableColumnName}"; //Value for items displayed in combo box. 
+0

似乎像炸彈一樣工作! – Eon

+0

我用「StockID」取代了displayMember和valuemember--我的數據集中唯一的一列 – Eon

+0

甚至更​​好。我只使用了valuemember。 2行瘋狂! – Eon

相關問題