-2

i am developing a wpf application , in which when the user performs selection changed action on a combobox , selection changed event is triggered, but in the event handler when i update datagrid.itemsSource, error is shown to use a new key word to create the object, through break point i have seen that inside the event handler datagrid object itself is null, but it is not null in the MainWindow function. why is it so?

    private void majorkeys_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {           
        minordata.ItemsSource = ReadLibrary.get_minor_records(majorkeys.SelectedValue.ToString(), minorkeys.SelectedValue.ToString());
    }
Sabeen
  • 53
  • 5

1 Answers1

0

Let me answer my own question, the problem was that when the component is initialized, comboBox.selectedIndex changes and hence comboBox_selectionChanged event is triggered at that time my datagrid's itemsSource is not populated, hence it is null. i have solved the problem by checking if the datagrid's itemsSource is null or not. Here is the code,

private void minorkeys_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (minordata != null)
        {
            minordata.ItemsSource = ReadLibrary.get_minor_records(majorkeys.SelectedValue.ToString(), minorkeys.SelectedValue.ToString());
        }
    }

yay! :)

Sabeen
  • 53
  • 5