VIBlend
Home Products Downloads Purchase Support Forum About Blog

WPF DataGrid - data binding to SQL Server Database

by viblend 21. December 2010 19:23

One of the most common data binding scenarios is to bind a DataGrid to a SQL Server Database and to load records from a specified data table. In this post, we will explain you how to set up basic data binding in VIBlend WPF DataGrid to the Customer table of the AdventureWorksLT Database.

So the first step would be to create a new WPF Application Project and then create a new instance of the DataGrid via drag and drop from the toolbox or create it programmatically. The DataGrid fields that will represent data source fields are created in XAML markup. Below you will see that we have very simple XAML for our WPF application which adds a DataGrid to the window

       <viblendDataGrid:DataGrid ItemsSource="{Binding Customer}" Name="dataGrid1" Margin="10">
            <viblendDataGrid:DataGrid.BoundFields>
                <viblendDataGrid:BoundField Text="First Name" DataField="FirstName" Width="70"/>
                <viblendDataGrid:BoundField Text="Last Name" DataField="LastName" Width="70"/>
                <viblendDataGrid:BoundField Text="Company Name" DataField="CompanyName" Width="150"/>
                <viblendDataGrid:BoundField Text="Phone" DataField="Phone" Width="100"/>
                <viblendDataGrid:BoundField Text="Email Address" DataField="EmailAddress" Width="100"/>
            </viblendDataGrid:DataGrid.BoundFields>
        </viblendDataGrid:DataGrid>

 Next, create a new AdventureWorksLTDataSet. In order to create it, do the following: Select Data->Add New DataSource->DataBase->DataSet. Add a connection to the AdventureWorksLT Database and Click the 'Next' button. Then expand the 'Tables' tree node, select Customer and click the 'Finish' button.



Note: The AdventureWorksLT Database is installed with the installation of the SQL Server. However, you can also download and install it from http://sqlserversamples.codeplex.com 
Once the Data Source is configured, create new instances of the AdventureWorksLTDataSet data set. and CustomerTableAdapter , then fill the Customer table with data. Finally, bind the VIBlend DataGrid for WPF to the Customer table.

C#

   public partial class MainWindow : Window
    {
        AdventureWorksLTDataSet dataSet = new AdventureWorksLTDataSet();
        AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter adapter = new AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter();
   
        public MainWindow()
        {
            InitializeComponent();
            adapter.Fill(dataSet.Customer);
            this.dataGrid1.DataContext = this;
        }

        public WpfApplication2.AdventureWorksLTDataSet.CustomerDataTable Customer
        {
            get
            {
                return this.dataSet.Customer;
            }
        }
    }

VB .NET

   Partial Public Class MainWindow
       Inherits Window
        Private dataSet As New AdventureWorksLTDataSet()
        Private adapter As New AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter()

        Public Sub New()
            InitializeComponent()
            adapter.Fill(dataSet.Customer)
            Me.dataGrid1.DataContext = Me
        End Sub

        Public ReadOnly Property Customer() As WpfApplication2.AdventureWorksLTDataSet.CustomerDataTable
            Get
                Return Me.dataSet.Customer
            End Get
        End Property
   End Class

 

Comments are closed

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET