我尝试将DataGridView
与数据库绑定。
我们可以创建一个数据集,然后选择一个项目并将其拖动到我们的控件中。它将在后面的代码中创建绑定。我使用MVVM并且我不想打破这种模式。
XAML
<Window.Resources>
<DataSources:ServiceStationDBDataSet x:Key="serviceStationDBDataSet"/>
<CollectionViewSource x:Key="serviceViewSource" Source="{Binding Service, Source={StaticResource serviceStationDBDataSet}}"/>
</Window.Resources>
<DataGrid x:Name="DataGrid_Services"
Style="{StaticResource MainDataGrid}"
ItemsSource="{Binding Source={StaticResource serviceViewSource}}">
</DataGrid>
C#(代码隐藏)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ServiceStationDBDataSet serviceStationDBDataSet = ((ServiceStationDBDataSet)(this.FindResource("serviceStationDBDataSet")));
// Load data into the table Service. You can modify this code as needed.
ServiceTableAdapter DBDataSetServiceTableAdapter = new ServiceTableAdapter();
DBDataSetServiceTableAdapter.Fill(serviceStationDBDataSet.Service);
CollectionViewSource serviceViewSource = ((CollectionViewSource)(this.FindResource("serviceViewSource")));
serviceViewSource.View.MoveCurrentToFirst();
}
我想用我的MVVM类创建绑定DataGridView
。
我该怎么办?