break

WPF: Xceed DataGrid for WPF

The purpose of this post is to show you how to display a WPF Window with a datagrid that contains data from a SQL Server Database.

The version of Orcas I’m using doesn’t have a Datagrid between their tools. Consequently, I’m using Xceed’s Datagrid which according to them is “the world’s first data grid control for Microsoft’s Windows Presentation Foundation”. Very interesting!

I installed this datagrid, and at first I thought it would appear under my tools in Orcas. I thought this because i’m used to drag and drop my controls to the form. However, I later found out that to be able to display the grid in my WPF window, I needed to add the grid in the XAML file. This is an example:

Window1.xaml

<Window x:Class=”WPFTrading.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”
Title=”Window1″ Height=”250″ Width=”800″ WindowStyle=”ThreeDBorderWindow”>
<Grid Name=”dataGrid1″>
<xcdg:DataGridControl x:Name=”ordersGrid”
ItemsSource=”{Binding Source={x:Static Application.Current},Path=Orders}”>
</xcdg:DataGridControl>

</Grid>
</Window>

Now I’m going to explain the parts in bold:

xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”

This is xceed’s namespace. If you don’t have this line you will get a compiler error saying “”xcdg’ is an undeclared namespace.”

<xcdg:DataGridControl x:Name=”ordersGrid”
ItemsSource=”{Binding Source={x:Static Application.Current},Path=Orders}”>
</xcdg:DataGridControl>

This is xceed’s Datagrid. The name of my Datagrid is ordersGrid. In ItemsSource I’m basically binding the grid to the Orders table.

This is all the XAML you need for the Datagrid to appear in your window. However, at this point your datagrid has no data at all.

When you create a WPF Window project an App.xaml and App.xaml.cs files are created. This files are important because in the App.xaml you can do several things. One of them is to set in what window the application is going to start. Here’s an example:

App.xaml

<Application x:Class=”WPFTrading.App”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”
StartupUri=”Window1.xaml”>

</Application>

Now I’m going to explain the parts in bold:

StartupUri=”Window1.xaml”

As you can notice when my project starts, it loads Window1.xaml

At this point, I’m going to write about how to put some data in the datagrid. I did this in my App.xaml.cs. Here’s the pseudocode of it:

App.xaml.cs

private DataTable mOrders;
public DataTable Orders
{
get { return mOrders; }
}
protected override void OnStartup(StartupEventArgs e)
{
Xceed.Wpf.DataGrid.Licenser.LicenseKey = "Your key goes here";
// create a connection string
string connectionString = "Your connection string goes here";
using (SqlConnection connection = new SqlConnection(connectionString))
{
// open a connection
connection.Open();
// create a sqlcommand
SqlCommand command = new SqlCommand();
command.CommandText = "Your SELECT statement goes here";
// create a dataset
DataSet ds = new DataSet();
// create a sqldataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, connection);
// fill the adapter
adapter.Fill(ds);
// fill the orders table
mOrders = ds.Tables[0];
// close connection
connection.Close();
// raise the Startup event.
base.OnStartup(e);
}
}

Some observations:
- You need a key for your datagrid. If not you will get a LicenseException.
- Make sure you paste the code in notepad and not microsoft word, since you might have problems with the quotes. Make sure the quotes are correct.
- I didn’t post my connection string because it varies.

At the end I ended up with a datagrid like this:
datagrid

Pretty cool!!!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

CAPTCHA Image
Reload Image