我正在努力将数据集与WPF数据网格一起使用。绑定和填充网格非常容易,只需创建数据集数据源并将其拖到窗口上即可。瞧!但是当我尝试从数据集中附加RowChanged事件时,我收到错误:
已经有一个与此命令关联的打开DataReader,必须先关闭它。
当我在事件处理程序中放置一个断点时,由于某种原因,它会被调用两次(至少),并且在第二次调用时,上面的描述会发生异常。我多次调用它是没有意义的。
我已经读过我可以在我的连接字符串中添加“MultipleActiveResultSets = true”,但这感觉就像我正在使用SqlServer功能错误地掩盖了我做错的事情。
这是我的XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test" x:Class="Test.MainWindow"
Title="MainWindow" Height="350" Width="1141" Loaded="Window_Loaded">
<Window.Resources>
<local:SentinelDBDataSet x:Key="sentinelDBDataSet"/>
<CollectionViewSource x:Key="changeEventViewSource" Source="{Binding ChangeEvent, Source={StaticResource sentinelDBDataSet}}"/>
</Window.Resources>
<Grid DataContext="{StaticResource changeEventViewSource}">
<DataGrid x:Name="changeEventDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="0
" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="cameraIDColumn" Binding="{Binding CameraID}" Header="Camera ID" Width="SizeToHeader" IsReadOnly="True"/>
<!--<DataGridTextColumn x:Name="changeEventIDColumn" Binding="{Binding ChangeEventID}" Header="Change Event ID" IsReadOnly="True" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="backgroundID_RefColumn" Binding="{Binding BackgroundID_Ref}" Header="Background ID Ref" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="backgroundID_ChangeColumn" Binding="{Binding BackgroundID_Change}" Header="Background ID Change" Width="SizeToHeader"/>-->
<DataGridTemplateColumn x:Name="timeStampColumn" Header="Time Stamp" Width="SizeToHeader" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding TimeStamp, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="annotationColumn" Binding="{Binding Annotation}" Header="Annotation" Width="SizeToHeader" IsReadOnly="False"/>
<DataGridCheckBoxColumn x:Name="validatedColumn" Binding="{Binding Validated}" Header="Validated" Width="SizeToHeader" IsReadOnly="True"/>
<DataGridTextColumn x:Name="confidenceColumn" Binding="{Binding Confidence}" Header="Confidence" Width="SizeToHeader" IsReadOnly="True"/>
<!--<DataGridCheckBoxColumn x:Name="focusChangedColumn" Binding="{Binding FocusChanged}" Header="Focus Changed" Width="SizeToHeader"/>-->
<DataGridCheckBoxColumn x:Name="focusDeltaChangedColumn" Binding="{Binding FocusDeltaChanged}" Header="Focus Delta Changed" Width="SizeToHeader" IsReadOnly="True"/>
<!--<DataGridCheckBoxColumn x:Name="brightnessChangedColumn" Binding="{Binding BrightnessChanged}" Header="Brightness Changed" Width="SizeToHeader"/>-->
<DataGridCheckBoxColumn x:Name="correlationChangedColumn" Binding="{Binding CorrelationChanged}" Header="Correlation Changed" Width="SizeToHeader" IsReadOnly="True"/>
<DataGridCheckBoxColumn x:Name="edgeCorrelationChangedColumn" Binding="{Binding EdgeCorrelationChanged}" Header="Edge Correlation Changed" Width="SizeToHeader" IsReadOnly="True"/>
<DataGridCheckBoxColumn x:Name="edgeDensityChangedColumn" Binding="{Binding EdgeDensityChanged}" Header="Edge Density Changed" Width="SizeToHeader" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
这是我的MainWindow课程:
public partial class MainWindow : Window
{
SentinelDBDataSet dataset;
SentinelDBDataSetTableAdapters.ChangeEventTableAdapter adapter;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dataset = ((Test.SentinelDBDataSet)(this.FindResource("sentinelDBDataSet")));
// Load data into the table ChangeEvent. You can modify this code as needed.
adapter = new Test.SentinelDBDataSetTableAdapters.ChangeEventTableAdapter();
adapter.Fill(dataset.ChangeEvent);
System.Windows.Data.CollectionViewSource changeEventViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("changeEventViewSource")));
changeEventViewSource.View.MoveCurrentToFirst();
dataset.ChangeEvent.RowChanged += ChangeEvent_RowChanged;
}
void ChangeEvent_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
{
adapter.Update(dataset.ChangeEvent);
}
}
任何建议都将不胜感激。如果有一个更好的控件用于与DataSet交互,我愿意看看WPF带来的任何东西。不幸的是,该项目不支持使用Infragistics等商业工具。
布赖恩