Datagrid index out of range but it is full and selectable etc


Well-known member
Dec 5, 2012
Programming Experience
When trying to set anything using datagridname.Columns(0) I get index out of range yet there are several rows with several columns showing. It works on other Datagrids in my project but not this one. Here is the code

Datagrid code:
        sqltext = "
(select name from stock where as Name,
amount as Amount,
(select name from units where as Units,
 from recipeparts where recipeid=@param1 ORDER BY NAME;"
        dt = QueryParam(sqltext, recipeid)
        dgItems.ItemsSource = dt.DefaultView
        'dgItems.Columns(0).Visibility = Visibility.Collapsed (error here when not commented out)

Any clues?
Actually I am starting to thing the Datagrid control is simply unreliable in 2017. Forms that I had working perfectly start doing the same thing when I haven't even touched that code in days. I also had to use "SelectedItem" when I used to user "CurrentItem" and then have to change back due to errors suddenly popping up. I thought it was just me but another Datagrid starting spewing errors when it has been working fine all week.
Ahh I got it, the control is slower than the program execution and the columns don't exist yet when I try to set them. There is an event "AutoGeneratedColumns" that fires when they are completed and I moved all the settings there so everything is fine now. It was just me, not vstudio as per usual

Latest posts