I have a DataGrid. When I change one of the DataGrid's column names, I want an event to arise so that the column name of the DataTable (the ItemSource of DataGrid) changes as well.
How can I sync the column names of the DataGrid and DataTable?
EDIT
I have datagrid like this with 6 column:
<wpfc4:RGrid x:Name="dgrid" Grid.Row="2" ItemsSource="{Binding Model.CurrentDataTable}" Style="{DynamicResource DataGridRStyle}" AutoGenerateColumns="True"></wpfc4:RGrid>
This RemoveSelectedColumn function :
private void RemoveSelectedColumn()
{
DataGridColumn toRemove = this.Columns[_selectedColumnIndex];
DataView view = this.ItemsSource as DataView;
view.Table.Columns.Remove(toRemove.Header.ToString());
// deleting column from datatable.
this.UpdateLayout();
this.Items.Refresh();
}
It's deleting from datatable but datagrid still has 6 column but deleted column has blank value. How I cna update datagrid so it shows only 4 column when i deleted 2 columns from datatable.
If possible, I would try to reverse this so that any time you change the name of one of the columns in the
DataTable
, theDataGrid
gets updated.If you have
AutoGenerateColumns
set totrue
on your DataGrid, this should be quite straightforward:ItemsSource
property of the DataGrid).INotifyPropertyChanged
, then raise thePropertyChanged
event any time a column name on your DataTable changes.