Dynamically populate lists by information from other conditions — WinForms

We will discuss this problem and its solution by example. Let's say the user can define a condition with city e.g. “City name is equal to LA”. But if the user has already selected a country (or a few countries) in one of their condition lists then the list of cities available for indicated condition must be filled with only the cities in the selected countries.

To implement such functionality, you just need to monitor all changes in a query and when the “Country” field is selected in some condition - you should set some variable and then use this variable when generating the list of Cities. To monitor all changes you can use ConditionsChanged event of Query object.

So, here our code sample.

First of all, we save an attribute which should be monitored into the particular variable. After that you use that variable when populating “Cities” list:

  private DataModel.EntityAttr countryAttr = null;
  
  public MainForm()
  {
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  . . . . . . . 
  //somewhere at the form start
  countryAttr = model.EntityRoot.FindAttribute(EntAttrProp.SqlName, "Customers.Country");
  }
  
  //each time user updates the condition with monitored attribute,
  //we refresh all lists that depends on it
  private void query_ConditionsChanged(object sender, ConditionsChangeEventArgs e) {
  if (e.Condition.BaseAttr != null && e.Condition.BaseAttr == countryAttr) {
  QPanel.RefreshList("RegionList");
  }
  }
  
  private void QPanel_ListRequest(object sender, ListRequestEventArgs e) {
  if (e.ListName == "RegionList") {
  e.ListItems.Clear();
  string country = query.GetOneValueForAttr(countryAttr);
  
  if (country == "Canada") { 
  e.ListItems.Add("British Columbia", "BC");
  e.ListItems.Add("Quebec", "Quebec");
  }
  else if (country == "USA") {
  e.ListItems.Add("California", "CA");
  e.ListItems.Add("Colorado", "CO");
  e.ListItems.Add("Oregon", "OR");
  e.ListItems.Add("Washington", "WA");
  }
  }