Dynamic add columns to a datagrid at runtime

Sennevds

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.

Sennevds

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related