I have finished a code sequence to insert data from a DataSet into a combobox, but it seemed like an awful lot of work. I thought there may have been a shorter way, then I thought about this question. Is it possible to convert a set of rows in a column of a table in a dataset to a list which can be entered into a combobox with minimal coding effort?
The approaches I have tried was to use the Dataset.Tables[0].Select() to return a dataRowCollection, but that idea crashed and burned – the combobox didn’t want to accept it.
The other approach I tried worked, but it seemed like a similar approach I had with my longer code:
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
}
But the code above did not answer my question. This is where I rephrase my question: Is it possible to convert a set of rows in a column of a table in a dataset to a list which can be entered into a combobox with minimal coding effort?
Is there a reason you can’t use common data binding in your scenario? This should work: