Download the Solution Example here: ComboBoxFromTable.dbsln
Software Version: 10.1
Summary
This solution demonstrates how to populate ComboBoxes using a data table.
Technical Information
First, you need to obtain control of the ComboBox on the display. This is achieved through the following lines:
ComboBox cbxListCodes; public void DisplayOpening() { cbxListCodes = CurrentDisplay.GetControl("cbxListCodes") as ComboBox; }
After that, you need to load your data table. In this example, we will retrieve the entire table asynchronously.
// Selects the whole table @Dataset.Table.Products.WhereCondition = ""; DataTable dt = await @Dataset.Table.Products.SelectCommandAsync();
Next, you can use a simple foreach loop to select which rows to add to the ComboBox, as shown in the code snippet below.
// Inserts each row to the combobox foreach (DataRow row in dt.Rows) { cbxListCodes.Items.Add(row["ListCode"]); }
Finally, using an EventHandler, you can determine the value selected by the client from the ComboBox and update the other TextBoxes accordingly.
cbxListCodes.SelectionChanged += new SelectionChangedEventHandler(codeChanged); public void codeChanged(object sender, SelectionChangedEventArgs args) { if(cbxListCodes.SelectedValue == null) return; //Selecting from the DB only the Item corresponding to the Code Selected in the ComboBox @Dataset.Table.Products.WhereCondition = "ListCode = " + cbxListCodes.SelectedValue.ToString(); @Dataset.Table.Products.SelectCommandAsync(); }
Reference Information
→ See "Displays and Visualization” for more information.
In this section: