Sennevds
Sennevds

Reputation: 83

Dynamic add columns to a datagrid at runtime

I've a datatable with certain columns and bind this to a datagrid with autogeneratecolumns which worsk perfectly. No when a user pushes a button another datatable is created and merged with the original DataTable. The rows are added but the new datatable has extra columns. These columns don't show in the datagrid. I've tried UpdateLayout on the datagrid, resetting the itemssource but nothing works.

How can I let the datagrid to regenerate?

The columns need to be auto generated because I never know how many columns there are going to be.

Upvotes: 1

Views: 271

Answers (1)

Sennevds
Sennevds

Reputation: 83

owkey found out a solution. i've created an eventhandler in the viewmodel and listen in the view for this event. When the new data is set I fire the event. In the code of the view I set the ItemSource of the datagrid to null and back to my datatable. Then the columns are regenerated

Edit: This works once better solution is instead of resetting the itemssource is the follwoing:

dataGrid.Items.Refresh();
dataGrid.AutoGenerateColumns = false;
dataGrid.AutoGenerateColumns = true;
dataGrid.UpdateLayout();

Upvotes: 1

Related Questions