1

I'm trying to populate a datagrid calling a function with RoutedEventArgs event. So far it works, but I was expecting to reuse the function on a TabControl SelectionChanged, but when I reference the previous function, I get a NullReferenceException. I think the event was handled before on TabControl event, so I'm stuck on this puzzle, since how could I reap the previous event. Here are the functions:

private void BtnMostrar(object sender, RoutedEventArgs e)
{
    ObservableCollection<EventoRubrica> cltEvtRubrica =
        new ObservableCollection<EventoRubrica>();

    using (var db = new EventoRubricaContext())
    {
        var evts = db.EventoRubricas;
        foreach(var evt in evts)
        {
            cltEvtRubrica.Add(evt);
        }
    }

    evtDataGrid.ItemsSource = cltEvtRubrica;
}

private void tbCtrl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    BtnMostrar(sender, e);
}
David Borges
  • 105
  • 2
  • 11
  • You need to debug this and find out exactly which variable is `null`. See [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Charlieface Jan 03 '22 at 15:12
  • I developed a workaround populating the datagrid at start up and splitting the _BtnMostrar_ function to another function specifically return a _ObservableCollection_ (called from the MainWindow class as _this.FeedDataGrid()_) which is called by this _BtnMostrar_, and other functions responsable to modify the datagrid. – David Borges Jan 05 '22 at 14:42

0 Answers0