I have a table in a database which I want to display in a WPF DataGrid. However I want to hide the first column. This column defines Id's for all items. I need the Id's for further actions, but I don't want to show it in the DataGrid. I've tried the code below, but I do get an error, on the last line, that the index has to be bigger than 0.
DbMainTable.ItemsSource = dataHandler.visibleDatabaseTable.DefaultView;
DbMainTable.Columns[0].Visibility = Visibility.Hidden;
If anyone can help me, let me know.
Could you provide more information about this issue? It is hard to guess what part of code is not working based on this.
But if I had to guess you use automatically generated columns and you try to hide this column before it is added to array of columns.
I event tried to do this with autogenerated columns and it gives me the same exception as you get.
To resolve this move this part of code somewhere where this datagrid (and its columns) is already loaded - for example to OnLoaded event handler in code behind
To achieve this:
in code behind add this method
and in XAML: