This is a migrated thread and some comments may be shown as answers.

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

1 Answer 37 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Hazzard
Top achievements
Rank 1
Hazzard asked on 04 Oct 2011, 07:29 AM

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; }
}

1 Answer, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 05 Oct 2011, 03:26 PM
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 >>
Tags
ComboBox
Asked by
Hazzard
Top achievements
Rank 1
Answers by
Konstantina
Telerik team
Share this question
or