92AlanC
92AlanC

Reputation: 1387

Programmatically add new row to WPF DataGrid

I'm trying to add new rows to a DataGrid on a WPF application when clicking on a specific button. This is what I tried so far:

DataGridRow row = new DataGridRow();
table.Items.Add(row); // table = my DataGrid

When I run this code, it throws a System.ArgumentNullException saying

Value cannot be null

What should I do to solve this problem? Thanks in advance.

Upvotes: 1

Views: 1793

Answers (1)

Dean Kuga
Dean Kuga

Reputation: 12119

You need to bind your Data Grid's ItemsSource property to a collection of POCO objects and instead of adding Data Grid rows add new POCO objects to that collection and your "problem" will be automagically resolved...

This assumes change notification of your VM properties and implementing INotifyPropertyChanged in your POCO classes of course...

Upvotes: 1

Related Questions