In the last step you set up the grid application – but while the basic grid is functional, it contains no data. In this step you'll continue in Visual Studio by adding a data source to your project. You'll then open the project in Microsoft Expression Blend to complete binding the grid to the data source.
To add a data source and set up data binding in Visual Studio, complete the following steps:
If the Choose a Database Model screen appears, select Dataset and click Next.
Visual Basic Copy Code Imports C1.WPF.DataGrid Imports ProjectName.ProductsDSTableAdapters
C# Copy Code using C1.WPF.DataGrid; using ProjectName.ProductsDSTableAdapters;
Visual Basic Copy Code Class MainWindow Inherits Window Private _productsDataSet As ProductsDS = Nothing Public ReadOnly Property ProductsDataSet() As ProductsDS Get If _productsDataSet Is Nothing Then _productsDataSet = New ProductsDS() Dim prodTA As New ProductsTableAdapter() prodTA.Fill(_productsDataSet.Products) End If Return _productsDataSet End Get End Property Public Sub New() InitializeComponent() End Sub End Class
C# Copy Code public partial class MainWindow : Window { private ProductsDS _productsDataSet = null; public ProductsDS ProductsDataSet { get { if (_productsDataSet == null) { _productsDataSet = new ProductsDS(); ProductsTableAdapter prodTA = new ProductsTableAdapter(); prodTA.Fill(_productsDataSet.Products); } return _productsDataSet; } } public MainWindow() { InitializeComponent(); } }
Visual Basic Copy Code Public Sub New() InitializeComponent() Me.C1DataGrid1.ItemsSource = ProductsDataSet.Products End Sub
C# Copy Code public MainWindow() { InitializeComponent(); this.c1DataGrid1.ItemsSource = ProductsDataSet.Products; }This code will bind the grid to the Products table in the C1NWind database.
Notice in the XAML view, the C1DataGrid tag now appears as the following:
XAML |
Copy Code
|
---|---|
<c1:C1DataGrid HorizontalAlignment="Left" Name="C1DataGrid1" VerticalAlignment="Top" Height="215" Width="384"/> |
Run the program and observe:
The grid is now populated with data from the Products table:
You've successfully bound DataGrid for WPF's C1DataGrid control to a data source. In the next step you'll explore some of the run-time interactions that are possible in your grid application.