带有DataView的DataContext的Grid在DataTable.Clear和DataAdapter.Fill之后不会显示数据
我会提前为很长的帖子道歉。我有两个 DataTable(案例和作业),每个都有一个我将 GUI 绑定到的 DataView(案例数据视图是网格的 DataContext,而作业数据视图是 ListView 的 ItemsSource 和 TabControl 的 DataContext)。我第一次使用 DataAdapter.Fill 填充表格时,案例和作业的数据正确显示。第二次加载数据时,我调用 DataTable.Clear,然后调用 DataAdapter.Fill,但 GUI 中仅显示作业数据。尽管单步执行,我可以看出数据表中有一行包含正确的数据,但案例数据却无处可见。即DataTable.Clear和DataAdapter.Fill工作正常;我的 GUI 中的网格控件不显示数据。我复制了下面网格控件的 XAML。有人可以帮助我吗?谢谢。
更新:如果我第二次在表中有多于一行,数据会正确显示在文本框中!到底是怎么回事?
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
微软的代码中肯定有一个错误,因为我用下面所示的代码修复了它。
我只需要对案例表执行此操作,而不是对作业表执行此操作,这就是为什么我预计某个地方存在错误。无论表中有一行或多于一行,作业表始终有效。
There has to be a bug in Microsoft's code somewhere, because I fixed it with the code shown below.
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.