Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


false
Info

Built with v10.

This solution demonstrates how to populate ComboBoxes using a data table.

icon

Download the Solution Example:

Solution file: ComboBoxFromTable.dbsln
Database file:ComboBoxFromTable.db

  • Solution Name: Combo Box From Table
  • Software Version: v10
  • Keywords: Displays. ComboBox. Datasets.



    Summary

    This solution demonstrates how to populate ComboBoxes using a data table.

    Image Modified


    Technical Information

    First, you need to obtain control of the ComboBox on the display. This is achieved through the following lines:


    Code Block
    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.


    Code Block
    // 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.

    Code Block
    // Inserts each row to the combobox
    foreach (DataRow row in dt.Rows)
    {
        cbxListCodes.Items.Add(row["ListCode"]);
    }
    
    


    Now, using an EventHandler, you can determine the value selected by the client from the ComboBox and update the other TextBoxes accordingly but this time, we'll use a different method to also retrieve the status of the SelectCommand, enabling easier troubleshooting if any issues occur. To achieve this, we'll create a new variable, "status," which will be passed by reference. This way, we'll change the SelectCommand method to SelectCommandWithStatusAsync.

    Finally, we retrieve the status value using a tag, allowing us to monitor the status directly in the property watch.

    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
        // Due to dealing with async methods that can't have multiple outputs
    	// it's used a workaround by passing a reference to an int.
        // Therefore, we use this reference to be an input for our SelectCommandWithStatusAsync() method
        // and retrieve its value by getting the Value property.
    
    TRef<int> status = new TRef<int>(); 
    @Dataset.Table.Products.WhereCondition = "ListCode = " + cbxListCodes.SelectedValue.ToString();
    if(@Dataset.Table.Products.SelectCommandWithStatusAsync(status).IsCompleted) 
    	{
    		@Tag.Status = status.Value;
    	} 
    }



    Reference Information

    → See Displays (Desktop and Web) for more information.

    → See ComboBox Control for more information.

    → See Datasets Tables for more information.


    In this section:

    Page Tree
    rootSolution Examples
    spacesV10