Learn more about Xceed DataGrid for WPF
Welcome to this new series. This time we will look at the Xceed DataGrid for WPF.
We will start slow with the basics, then cover various topics and features to learn how to make the most of the DataGrid in your projects.
Initial Setup
Let’s start by creating a new WPF application project and adding the references to the assemblies we will need.
There are assemblies for ThemePacks and for 3D Views, but we will keep those for a later time.
For now we will only add the following assemblies:
- Xeed DataGrid for WPF
- Xceed Controls for WPF
- Xceed.Wpf.DataGrid.Samples.SampleData
Namespace Mapping
Once the assemblies have been added to our new project, the namespace maps that are to be used must be declared.
In XAML this is done using the xmlns attribute:
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
Note: If the DataGridControl control has been added to a design surface, the xmlns attribute is automatically added.
The schema collection for Xceed DataGrid for WPF holds many namespaces. Using statements can be used to create aliases for the namespaces we want to use in our project.
using Xceed.Wpf.DataGrid;
using Xceed.Wpf.DataGrid.Converters;
using Xceed.Wpf.DataGrid.Print;
using Xceed.Wpf.DataGrid.Stats;
using Xceed.Wpf.DataGrid.ValidationRules;
using Xceed.Wpf.DataGrid.Views;
using Xceed.Wpf.DataGrid.Views.Surfaces;
using Xceed.Wpf.Controls;
using Xceed.Wpf.DataGrid.ThemePack;
using Xceed.Wpf.DataGrid.Settings;
using Xceed.Wpf.DataGrid.Export;
Binding
Finally, we will add a grid to our page or window. The examples found throughout the documentation usually place the grid inside a Grid, as demonstrated below.
Example 1: how to create a connection to the Access version of the Northwind database and create a property named “Orders” to which the grid will be bound.
using System.Data.OleDb;
using System.Data;
static App()
{
DataSet dataSet = new DataSet();
string mdbFile = @"DataNorthwind.mdb";
string connString = String.Format( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}", mdbFile );
OleDbConnection conn = new OleDbConnection( connString );
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand( "SELECT * FROM Orders;", conn );
adapter.Fill( dataSet, "Orders" );
m_orders = dataSet.Tables[ "Orders" ];
}
public static DataTable Orders
{
get
{
return m_orders;
}
}
private static DataTable m_orders;
Example 2: how to bind a grid to the Orders table, which is retrieved through the Orders property implemented in the code above.
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current},
Path=Orders}"/>
</Grid.Resources>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</Grid>
dataGridControl.ItemsSource = new DataGridCollectionView( App.Orders.DefaultView );
By default, a grid will take all the room that it requires; therefore, if it is not given a size constraint, such as when it is placed in a StackPanel, and a large amount of data items are present, UI virtualization will be lost, resulting in a significant loss in performance.
To preserve UI virtualization when a grid is in a StackPanel, the MaxWidth and MaxHeight properties (or Width and Height) must be used to constrain the grid. As an alternative, a DockPanel or Grid can be used as both impose size constraints on their child elements.
Licensing
The last step is to license Xceed DataGrid for WPF by setting the LicenseKey property.
There are 2 possible licensing scenarios:
- Using the trial mode.
- All features are available and can be evaluated for the duration of the trial period, after which an exception will be thrown. To use this mode, no license key is required.
- Licensing the component with a non-trial key (starts with DGP)
- Product license keys permanently unlock the component’s complete feature set
Note: the version of the license key must correspond to the version of the assemblies.
The LicenseKey property of the Licenser class must be set with a valid license key before any other method of the Xceed component is called. If an invalid license key or expired trial key is used, or it is licensed in the wrong place in the code, an exception will be thrown at runtime.
It is recommended that the LicenseKey property be set in one of the following locations:
- The preferred location to set the LicenseKey property is in the Application.OnSatartup method.
- The LicenseKey property can also be set in the Window.Loaded or Page.Loaded events, as long as the startup page or window is known and invariable.
Setting the LicenseKey property of the Licenser class defined in the Xceed.Wpf.DataGrid assembly will automatically license the classes in the Xceed.Wpf.Controls assembly.
The following code demonstrates how to set the LicenseKey property in the Application.OnStartup method override.
protected override void OnStartup( StartupEventArgs e )
{
Xceed.Wpf.DataGrid.Licenser.LicenseKey = "DGP70-xxxxx-xxxxx-xxxx";
base.OnStartup( e );
}
Example
While it is possible to specify which item properties from the data source should be included, and which columns should be displayed, we will look into those options later. For this example, we will let the grid automatically load all the item properties found in the data source, and create all the corresponding columns in the DataGrid.
<Window.Resources>
<xcdg:DataGridCollectionViewSource x:Key="mySource"
AutoCreateItemProperties="True"
Source="{Binding Source={x:Static Application.Current}, Path=RecordData}" />
</Window.Resources>
<Grid>
<xcdg:DataGridControl x:Name="myGrid"
AutoCreateColumns="True"
ItemsSource="{Binding Source={StaticResource mySource}}" >
</xcdg:DataGridControl>
</Grid>
Note: both the AutoCreateItemProperties property on DataGridCollectionViewSource, and the AutoCreateColumns property on DataGridControl, are true by default. Had they not been specified, the end result would have been the same.
For more information, please refer to the documentation.