This example shows how to display data from a database in a DataGrid.
A table from an existing local database is dragged and dropped into the WPF application.
In the first step, drag a DataGrid into the WPF application under Toos-> DataGrid
Then select from Database (Database, Service, Object)
Then choose the Database Model.
The database model later displays the structure of the database as a dataset.
As selection existing data connections are displayed. The local database (service based) is suggested as default.
.mdf files are sql server databases.
Afterwards one selects all tables and views, which one wants to take over in the project
Data sources
Then, under Data Sources, the database is displayed as a DataSet with the individual tables
DataSource: compile project.
In order for the DataSource to be displayed and integrated, you first have to compile the project
Now you just have to pull from the area: DataSources-> the table into the DataGrid
design view
After dragging the Datasource table into Design-> Datagrid, the structure of the table is displayed at design time
F5 running time
During the compiled start with F5, an executable data grid table is created in the form where the data is directly linked to the table and equipped with the standard functions of a grid.
You can delete new data in the bottom line, change it.
Code for loading
The c # code for loading the data grid can be found under the load event of the MainWindows
Standard code under MainWindow.xal.cs
To load the DataSource into the data grid while loading the form
private void Window_Loaded(object sender, RoutedEventArgs e) {
wpf_demo_Datagrid_Db.dbGridTestDataSet dbGridTestDataSet = ((wpf_demo_Datagrid_Db.dbGridTestDataSet)(this.FindResource("dbGridTestDataSet"))); // Load data into the table tbl_GridTest. You can modify this code as needed. wpf_demo_Datagrid_Db.dbGridTestDataSetTableAdapters.tbl_GridTestTableAdapter dbGridTestDataSettbl_GridTestTableAdapter = new wpf_demo_Datagrid_Db.dbGridTestDataSetTableAdapters.tbl_GridTestTableAdapter(); dbGridTestDataSettbl_GridTestTableAdapter.Fill(dbGridTestDataSet.tbl_GridTest);
System.Windows.Data.CollectionViewSource tbl_GridTestViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tbl_GridTestViewSource"))); tbl_GridTestViewSource.View.MoveCurrentToFirst(); }
|