1

I have got a list view which populates my chart of accounts:

public class ChartOfAccounts
{
    public int AccountCode { get; set; }
    public string AccountTitle { get; set; }
    public string Description { get; set; }
    public string SubCategory { get; set; }
    public string Category { get; set; }
    public bool Active { get; set; }
}

Through this list view, I want to populate other controls like:

private void MainRadDataGrid_SelectionChanged(object sender, Telerik.UI.Xaml.Controls.Grid.DataGridSelectionChangedEventArgs e)
{
    RadDataGrid rdg = (RadDataGrid)sender;

    var SelectedCOA = (ChartOfAccounts)rdg.SelectedItem;

    if (rdg !=null && rdg.SelectedItems.Count > 0) {
        AccountCodeTextBox.Text = SelectedCOA.AccountCode.ToString();
        AccountTitleTextBox.Text = SelectedCOA.AccountTitle;
        DescriptionTextBox.Text = SelectedCOA.Description;
        CategoryComboBox.SelectedItem = SelectedCOA.Category;

        SubCategoryComboBox.SelectedItem = SelectedCOA.SubCategory;
    }
}

The problem is, that I could not set the Category and SubCategory Comboboxes to the related Category and SubCategory. ComboBox only shows Category and Sub Category word, not the actual selected item.

Can anyone explains why this is not working?

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
M. Akbar
  • 11
  • 2
  • What do you set to CategoryComboBox.ItemsSource? – Martin Zikmund Sep 13 '18 at 11:55
  • Possible duplicate of [How do I set the selected item in a comboBox to match my string using C#?](https://stackoverflow.com/questions/450059/how-do-i-set-the-selected-item-in-a-combobox-to-match-my-string-using-c) – Adam Ostrožlík Sep 13 '18 at 12:03
  • You cannot set the SelectedItem Directly without setting the ItemsSource. Also See [this](https://stackoverflow.com/questions/46978988/set-combobox-item-in-code-behind-uwp/46980423#46980423) answer on how to properly select the item. – AVK Sep 13 '18 at 13:08

1 Answers1

1

I think that your answer is this:

CategoryComboBox.SelectedItem = Combox1.FindStringExact(SelectedCOA.Category.?) // ? = displayed cat name

or

CategoryComboBox.SelectedIndex = CategoryComboBox.Items.IndexOf(SelectedCOA.Category.?);
AVK
  • 3,893
  • 1
  • 22
  • 31
Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16