creating combobox values at UI level, not in db lookup table

2 posts, 0 answers
  1. Hazzard
    Hazzard avatar
    24 posts
    Member since:
    May 2006

    Posted 04 Oct 2011 Link to this post

    Here's a combobox and a look up table I initially created. Because there are only 3 values that will never change I was told to NOT use a lookup table but to populate the combobox with these 3 values at the UI level and persist the relevant key value to the db so that the next time the application loads, whatever the user selected previously will be displayed. My current code is at bottom. Thank you!

    subacct dropdown                     SubAcct LookUpTable values

     

    <telerik:RadComboBox x:Name="cmbsubAcct" 
                         ItemsSource="{Binding RequestRecord.SubAccount, Mode=TwoWay}"
                         Loaded="cmbsubAcct_Loaded"
                         SelectionChanged="cmbsubAcct_SelectionChanged" />
    
    private void CallBack(object sender, CompletedEventArgs args)
    {
        var items = args.Result as MyRequestRecord;
        RequestRecord = new MyRequestRecord();
        RequestRecord = items;
    }
    
    private MyRequestRecord requestRecord;
    [Required]
    public MyRequestRecord RequestRecord
    {
        get { return requestRecord; }
        set
        {
            if (requestRecord != value)
            {
                requestRecord = value;
                FirePropertyChanged("RequestRecord");
    
            }
        }
    }
    
    //This doesn't work because it can load these items more than once.
    private void cmbsubAcct_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
       this.cmbsubAcct.Items.Add("Process");
       this.cmbsubAcct.Items.Add("STAND/AFE");
       this.cmbsubAcct.Items.Add("Lab");
    }
    
    private void cmbsubAcct_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
    {
       // ??
    }
    
    //WCF Service Level
    [DataContract(Name = "MyRequestRecord")]
    public class MyRequestRecord
    {
        [DataMember]
        public string SubAccount { get; set; }
    }
    
  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 05 Oct 2011 Link to this post

    Hi Hazzard,

    You can add the items to the collection after the InitializeComponent()  in the constructor of the page. After that you can select the previously chosen element. In order to get it, you can save the SelectedIndex of the ComboBox on SelectionChanged event. You need to create a logic in order to connect the SelectedIndex and the index of the row in your table.
    The other option is to create a view model for the items of the ComboBox and each object to have 2 properties: the name and the code, which will match the table. After that put the items in a ObservableCollection and bind it to the ItemsSource of the ComboBox. You can find an example of this in this help article. In that way you can record the exact code of the items and match them in the data base.

    Hope this information helps.

    Kind regards,
    Konstantina
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  3. DevCraft banner
Back to Top