0

So I have an XML file which I read in then display the data onto a DataGrid. Once I bind the DataView of the XML file to the ItemsSource of the datagrid I then attempt to hide the 4th column that will be created (it's an ID which I don't want to show) but receive an error

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message='The invocation of the constructor on type 'MoneyLog.MainWindow' that matches the specified binding constraints threw an exception.' Line number '6' and line position '9'.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)

Inner Exception 1:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

The Code I use:

DataGrid.Columns[4].Visibility = Visibility.Collapsed;

From the error I can see that apparently my number (4) was out of range therefore I assume the table must not of been create yet. To fix this i wrote a crude hack that delays the chunk of code by half a second and this fixes the problem...?

DataGrid.ItemsSource = dataView;
var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) }; 
timer.Start();
timer.Tick += (sender, args) =>
{
     timer.Stop();
     DataGrid.Columns[3].Width = 250;
     DataGrid.Columns[4].Visibility = Visibility.Collapsed;
     DataGrid.CanUserAddRows = false;
     DataGrid.IsReadOnly = true;
};

My question is there another way to do this without this whole timer workaround?

  • Could you show the code where you bind Datasource to DataGrid? May be better to disable AutoGenerateColumns and add columns manually? – Павел Марченко May 18 '18 at 16:07
  • DataGrid.ItemsSource = dataView; It's the first line in the code above, after that i start a 500 millisecond time and only then can i edit the table properties like column width and visibility. – Chris Dworczyk May 19 '18 at 01:04
  • Did you tried to bound to the AutoGeneratedColumns event and hide column after all columns will be generated? I think you tried to change column that didn't generated yet. – Павел Марченко May 21 '18 at 08:18

0 Answers0