Info |
---|
Built with v10. This solution demonstrates how to populate ComboBoxes using a data table. Download the Solution Example |
: Solution file: ComboBoxFromTable.dbsln |
Solution Name: ComboBox from Table
Software Version: 10.1Database file:ComboBoxFromTable.db |
Summary
This solution is an example on demonstrates how to fill populate ComboBoxes through using a data table.
Technical Information
Initially, you need to gain control of the display's ComboBox. This is done with the following lines:
This example shows 2 options for populating a ComboBox.
Option 1: Adding the items in the CodeBehind
Option 2: Assigning a Tag to the ComboBox Items Source.
The easiest option is the second one, mapping tags directly to the Items Source, and to Selected Value, so no code is required. Open the example to see this configuration.
When using the CodeBehind, follow these steps:
1) Get The ComboBox form the displays
Code Block |
---|
ComboBox cbxListCodes;
public void DisplayOpening()
{
cbxListCodes = CurrentDisplay.GetControl("cbxListCodes") as ComboBox;
}
|
2) After that, you need to load your data table. In this example, we will retrieve the entire table asynchronously.
Code Block |
---|
// Selects the whole table
@Dataset.Table.Products.WhereCondition = "";
DataTable dt = await @Dataset.Table.Products.SelectCommandAsync();
|
3) Next, using you can use a simple foreach loop , you can select the desired rows and add them to select which rows to add to the ComboBox, as demonstrated shown in the code snippet below.
Code Block |
---|
// Inserts each row to the combobox
foreach (DataRow row in dt.Rows)
{
cbxListCodes.Items.Add(row["ListCode"]);
}
|
Finally4) Now, by using an EventHandler, you can determine which the value selected by the client selected from the ComboBox and use it to update the other TextBoxes.
Code Block |
---|
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(); string selectValue = (sender as ComboBox).SelectedValue; // you code } |
Reference Information
→ See "Displays and Visualization” (Desktop and Web) for more information.
→ See ComboBox Control for more information.
→ See Datasets Tables for more information.
In this section:
Page Tree | ||||
---|---|---|---|---|
|