Grid with DataContext of DataView won't display data after DataTable.Clear and DataAdapter.Fill
-
29-09-2019 - |
Question
I'll apologize in advance for a long post. I have two DataTables (Cases and Jobs), each with a DataView that I bind my GUI to (the Cases dataview is the DataContext for a Grid, while the Jobs dataview is the ItemsSource of a ListView and the DataContext of a TabControl). The first time I fill the tables with DataAdapter.Fill, the data shows properly for cases and jobs. The second time I load data, I call DataTable.Clear and then DataAdapter.Fill, but only the jobs data shows in the GUI. The case data is nowhere to be seen, even though stepping through I can tell there's a row in the DataTable with the correct data. That is, the DataTable.Clear and DataAdapter.Fill work properly; the Grid control in my GUI just doesn't show the data. I've copied the XAML for the Grid control below. Can anyone help me?! Thanks.
UPDATE: If I have more than one row in the table the second time around, data shows up in the text boxes properly! What the hell is going on?
<Grid Height="165" Width="390" DataContext="{Binding caseTableView}" >
<Grid.Resources>
<local:CaseStatusItemsSource x:Key="StatusesSource" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<!--Case Details-->
<Label Style="{StaticResource LabelStyle}" Grid.Row="1" Grid.Column="1" >Case Number:</Label>
<TextBox Grid.Row="1" Grid.Column="2" Style="{StaticResource TextBoxStyle}" IsReadOnly="{Binding isROCaseNumber}" LostFocus="caseNumber_LostFocus" >
<TextBox.Text>
<Binding Path="/CASENUMBER" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:CaseNumberValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Style="{StaticResource LabelStyle}" Grid.Row="2" Grid.Column="1" >Date Received:</Label>
<TextBox Grid.Row="2" Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=/DATERECEIVED, StringFormat=d}" IsReadOnly="{Binding isRODateReceived}" />
<Label Style="{StaticResource LabelStyle}" Grid.Row="3" Grid.Column="1" >Status:</Label>
<ComboBox Grid.Row="3" Grid.Column="2" Width="140" Height="20" HorizontalAlignment="Left" FontFamily="Verdana" FontSize="9"
ItemsSource="{Binding statuses, Source={StaticResource StatusesSource}}" SelectedItem="{Binding Path=/STATUS, Mode=TwoWay}" IsReadOnly="{Binding isROCaseStatus}" >
</ComboBox>
<Label Style="{StaticResource LabelStyle}" Grid.Row="4" Grid.Column="1" >Date Ord. Received:</Label>
<TextBox Grid.Row="4" Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=/DATEORDRECEIVED, StringFormat=d}" IsReadOnly="{Binding isRODateOrdReceived}" />
<Label Style="{StaticResource LabelStyle}" Grid.Row="5" Grid.Column="1" >Date Posted:</Label>
<TextBox Grid.Row="5" Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=/DATEPOSTED, StringFormat=d}" IsReadOnly="{Binding isRODatePosted}" />
</Grid>
Solution
There has to be a bug in Microsoft's code somewhere, because I fixed it with the code shown below.
ICollectionView _cv = CollectionViewSource.GetDefaultView(_appModel.casesTable);
_cv.MoveCurrentToNext();
I only needed to do this for the case table, not the jobs table, which is why I expect there's a bug somewhere. The jobs table always worked whether there was one or more than one row in the table.