Click here to Skip to main content
15,881,424 members
Articles / Desktop Programming / XAML

WPF and SQLite Database

Rate me:
Please Sign up or sign in to vote.
3.92/5 (9 votes)
3 Feb 2011CPOL3 min read 132.8K   10.8K   33   14
Master-Detail binding in WPF to SQLite database
aplication_image.jpg

Introduction

After reading a lot of a good stuff about SQLite, I’ve decided to setup a simple WPF project which will handle Master-Detail relationship between two DataGrids and the SQLite database under the hood. This is a beginner article, so I am not going to dive into database layers, LINQ, etc.

Requirements

To build the article project, you will need the following:

Install the ADO.NET 2.0 Provider for SQLite, and make sure to install SQLite Designer (which is included in the package); this way, you can manage the database directly in your VS 2010 environment. Unzip and copy the SQLite Northwind database, preferably into your project folder.

Getting Started

Provided that you have successfully installed the above packages, create a WPF project, name it as you wish, and “throw” two DataGrids on your main window (MainWindow.xaml). At this point, I am not interested in styling my application, rather focus on its functionality.

Before proceeding any further, set the DataGrid's AutoGenerateColumns property to true; this will automatically generate a column for each field. For this case, it is quite useful; however, if you plan to make a full blown application, this approach does not give much control over the generated column.

Let the fun begin! On the Data Source panel, click Add New Data Source. Select the Database, next Dataset and finally create New Connection. Change the data to SQLite Database file, browse for the file and finally check Test Connection. If everything is OK, you should have connected successfully. The next step is to create “NorthwindDataSet” to which we will be referring afterwards.

SQLite_select.jpg

WPF C# Code Behind

Let’s add some code to our application. In our example, we are going to have two tables “Customers” and “Orders”. Each time the client click on “CustomerDataGrid, the detail DataGrid Orders” is going to be updated automatically.

Enter the following code to your MainWindow.xaml.cs:

C#
NorthwindDataSet ds;

// Private var
 
public MainWindow()
{
    InitializeComponent();

    //Create new DataSeta
    ds = new NorthwindDataSet();

    //Create table adapter for Customers
    NorthwindDataSetTableAdapters.CustomersTableAdapter customerAdapter = 
            new NorthwindDataSetTableAdapters.CustomersTableAdapter();

    //Populate adapter
    customerAdapter.Fill(ds.Customers);

    //Bind adapter to datagrid
    dataGrid1.ItemsSource = ds.Customers.DefaultView;

    //Populate the second datagrid "Orders"
    NorthwindDataSetTableAdapters.OrdersTableAdapter orderAdapter = 
          new NorthwindDataSetTableAdapters.OrdersTableAdapter();
    orderAdapter.Fill(ds.Orders);
    dataGrid2.ItemsSource = ds.Orders;
}

Next, double click on first datagrid Customers”. The method “dataGrid1_SelectionChanged” should be created meaning that every time you change the selection, this method will be invoked. I must admit that I’ve searched a lot for an easy way to create master-detail relationship in WPF between DataGrid’s controls, but with no avail. So I came up with the following code:

C#
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Get selected Row
    DataRowView row = (DataRowView) dataGrid1.SelectedItem;

    //Get selected Row Cell base on which the datagrid will be changed
    string customerId = row.Row["CustomerID"].ToString();

    //Check if everything is OK
    if (customerId == null || customerId == string.Empty)
    {
        return ;
    }

    //Change view based on RowFilet
    DataView view = ds.Orders.DefaultView;
    view.RowFilter = string.Format("CustomerID='{0}'", customerId);
    
}

The idea is to find the selected “CustomerID” in “CustomersDatagrid and then filter the “OrdersDatagrid based on that selection. I think the above code is self-explanatory. The full code looks like this:

C#
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
 
namespace SQLite_demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        NorthwindDataSet ds;
 
        public MainWindow()
        {
            InitializeComponent();
 
            //Create new DataSeta
            ds = new NorthwindDataSet();
 
            //Create table adapter for Customers
            NorthwindDataSetTableAdapters.CustomersTableAdapter customerAdapter = 
                      new NorthwindDataSetTableAdapters.CustomersTableAdapter();
 
            //Populate adapter
            customerAdapter.Fill(ds.Customers);
 
            //Bind adapter to datagrid
            dataGrid1.ItemsSource = ds.Customers.DefaultView;
 
            //Populate the second datagrid "Orders"
            NorthwindDataSetTableAdapters.OrdersTableAdapter orderAdapter = 
                     new NorthwindDataSetTableAdapters.OrdersTableAdapter();
            orderAdapter.Fill(ds.Orders);
            dataGrid2.ItemsSource = ds.Orders;
        }
 
        private void dataGrid1_SelectionChanged
		(object sender, SelectionChangedEventArgs e)
        {
            //Get selected Row
            DataRowView row = (DataRowView) dataGrid1.SelectedItem;
 
            //Get selected Row Cell base on which the datagrid will be changed
            string customerId = row.Row["CustomerID"].ToString();
 
            //Check if everything is OK
            if (customerId == null || customerId == string.Empty)
            {
                return ;
            }
 
            //Change view based on RowFilet
            DataView view = ds.Orders.DefaultView;
            view.RowFilter = string.Format("CustomerID='{0}'", customerId);
        }
    }
}

Addition Information

If you are developing against .??? 4 Framework, and you should do so, then it is necessary to add the following lines to your “app.config”.

XML
<configuration>
    <configsections>      
    </configsections>
  <startup uselegacyv2runtimeactivationpolicy="true">
    <supportedruntime version="v4.0">
  </supportedruntime></startup>
</configuration>

This way, you will avoid nasty compatibility errors between .NET 4.0 and .NET 2.0. This is happening because ADO.NET 2.0 Provider for SQLite is built against version .NET 2.0. Another solution would be to download the source code and recompile it for .NET 4.0, I never tried it so it is totally up to you.

Conclusion

This is a basic introduction on using SQLite databases with WPF. With these two, you can give to your users an alternative of using non-Microsoft database with the same, or maybe greater performance.

History

  • 3rd February, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) dinem.co.uk
United Kingdom United Kingdom
Dimitris is founder of www.dinem.co.uk. His interests includes new generation applications, web development, Search Engine Optimization (SEO) and Web Marketing. (Amongst others, I also studied Chemical Engineering and piano)

Comments and Discussions

 
BugHard Coded File-path in app.config Pin
Member 1157983027-Dec-17 16:43
Member 1157983027-Dec-17 16:43 
GeneralRe: Hard Coded File-path in app.config Pin
Member 1157983027-Dec-17 16:44
Member 1157983027-Dec-17 16:44 
GeneralMy vote of 3 Pin
JanHalfar23-Jul-14 6:29
JanHalfar23-Jul-14 6:29 
Questionhttp://sqlite.phxsoftware.com/ not found Pin
kiquenet.com31-Jan-14 5:54
professionalkiquenet.com31-Jan-14 5:54 
AnswerRe: http://sqlite.phxsoftware.com/ not found Pin
Dimitris Nemtsov31-Jan-14 6:58
Dimitris Nemtsov31-Jan-14 6:58 
QuestionUpdates for .NET 4.5.1 ? Pin
kiquenet.com31-Jan-14 4:05
professionalkiquenet.com31-Jan-14 4:05 
AnswerRe: Updates for .NET 4.5.1 ? Pin
Dimitris Nemtsov31-Jan-14 7:00
Dimitris Nemtsov31-Jan-14 7:00 
GeneralMy vote of 5 Pin
SinisaRuzin28-Jun-12 5:34
SinisaRuzin28-Jun-12 5:34 
QuestionXamlParseException Occured Pin
Krischu12-Oct-11 5:35
Krischu12-Oct-11 5:35 
AnswerRe: XamlParseException Occured Pin
Krischu17-Oct-11 6:13
Krischu17-Oct-11 6:13 
GeneralRe: XamlParseException Occured Pin
krishnakumartm21-Oct-11 2:43
krishnakumartm21-Oct-11 2:43 
GeneralRe: XamlParseException Occured Pin
Krischu21-Oct-11 3:55
Krischu21-Oct-11 3:55 
You should use Northwind.db3 (796KB) (which comes with the obave examples' .zip file).
To be sure, check with some DB-Browser (sqliteadmin), whether the table ORDERS has DATE type
and not TIMESTAMP in it's respective date fields (OrderDate, ShippedDate, RequiredDate).

Otherwise you may be facing a different problem.
--
Christoph
GeneralMy vote of 5 Pin
Basarat Ali Syed3-Feb-11 15:21
Basarat Ali Syed3-Feb-11 15:21 
GeneralRe: My vote of 5 Pin
Dimitris Nemtsov18-May-11 2:47
Dimitris Nemtsov18-May-11 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.