Erik
Erik

Reputation: 2436

WPF Datagrid: Update itemssource the simple way

first of all, I normally write code using MVVM or MVVMC, but for a very simple project, I want to try doing everything the "old way", meaning writing a simple to understand app using only code behind logic, and no INotifyPropertyChanged interface.

For that purpose, I have created a very simple Employee sample app with a class that loads a list of Employees to an Obersvablecolletion. The problem ist: after I set the Itemssource and DataContext, after Loading, my DataGrid does not get updated. Of course I could set the DataContext again after loading, but is there a better way to do so? Some kind of telling the DataGrid in code behind that its contents have changed and Invaldiate them?

Here is my sample code:

public partial class MainWindow : Window
{
    private EmployeeList _MyList;

    public MainWindow()
    {
        InitializeComponent();
         _MyList= new EmployeeList();            
         _MyList.Employees = new ObservableCollection<Employee>();                        
        _MyGrid.DataContext = _MyList;
        _MyGrid.ItemsSource = _MyList.Employees;
    }

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void _AddButton_Click(object sender, RoutedEventArgs e)
    {
        AddWindow newWindow = new AddWindow();
        if (newWindow.ShowDialog() == true)
        {
            _MyList.Employees.Add(newWindow.NewEmployee);
        }

    }

    private void _LoadButton_Click(object sender, RoutedEventArgs e)
    {
        _MyList.Load();            
        //Creates new_MyList.Employees and fills with content from a file. After that, my DataGrid does not get updated
    }

Upvotes: 0

Views: 2023

Answers (3)

itzmebibin
itzmebibin

Reputation: 9439

You can remove the existing item-source by assigning null value to the data-grid and then assign updated item-source like,

_MyGrid.ItemsSource = null;
_MyGrid.ItemsSource = _MyList.Employees;

Upvotes: 0

AnjumSKhan
AnjumSKhan

Reputation: 9827

If you are using ObservableCollection, then DataGrid will update itself without any extra issues.

But if you are using List<Employee>, then you need to re-assign the ItemsSource like below :

private void _AddButton_Click(object sender, RoutedEventArgs e)
    {
        AddWindow newWindow = new AddWindow();
        if (newWindow.ShowDialog() == true)
        {
            _MyList.Employees.Add(newWindow.NewEmployee);

            // re-assign the `ItemsSource`
            _MyGrid.ItemsSource = null;
            _MyGrid.ItemsSource = _MyList.Employees;
        }
    }

Upvotes: 0

Clemens
Clemens

Reputation: 128042

Do not create a new _MyList.Employees collection instance.

Instead clear and re-fill the existing one:

_MyList.Employees.Clear();

for (var employee in employeesFromFile)
{
    _MyList.Employees.Add(employee);
}

Since you aren't using a Binding for _MyGrid.ItemsSource, it's also not necessary to set _MyGrid.DataContext.

Upvotes: 2

Related Questions