WPF DataGrid Practical Examples- CodeProject

wpf advanced datagrid

The most common usage for the DataGrid is in combination with a database, but like most WPF controls, it works just as well with an in-memory source, like a list of objects. Since it’s a lot easier to demonstrate, we’ll mostly be using the latter approach in this tutorial. What sets FlexGrid apart from the competition is its blend of high performance and customization. It’s lightweight yet powerful, giving developers more control over how data is presented and managed.

  1. You may fix columns so user can’t resize them by setting the CanUserResizeColumns property to false.
  2. I also discussed how to format rows, columns, their visibility, and scrolling.
  3. It supports the same production-ready features as the WPF DataGrid control but also accelerates C# development with all the built-in features listed above.
  4. In Toolbox, you should see Infragistics 17.2 WPF category and all controls loaded below.
  5. That sounds convenient, but in reality, databases store strange fields like “last modification date” and odd look-up IDs that you don’t always want or need to display to the user.

Let’s replace the DataGrid control with FlexGrid, and then we’ll continue to customize it with advanced grouping and filtering. In your actual application, the data could be coming from a web service, SQL Server, JSON file or another source. In addition to displaying and editing data, datagrids can be great tools for basic data analysis.

wpf advanced datagrid

Key Features

Next, we’ll look at the full feature set for a typical C# or VB.NET datagrid and how the features compare between different grid controls. For example, the ComponentOne FlexGrid is a cross-platform .NET datagrid with a WPF version that supports .NET Framework 4.6.2 up through .NET 8 as of this writing. It supports the same production-ready features as the WPF DataGrid control but also accelerates C# development with all the built-in features listed above. The data items that are displayed in a grid can be filtered using the Filter property of the DataGridCollectionViewBase or the Filter event of the DataGridCollectionViewSourceBase to which it is bound.

You can customize the appearance and behavior of the data grid by setting various properties in the XAML code. You may fix columns so user can’t resize them by setting the CanUserResizeColumns property to false. The following code snippet sets CanUserResizeColumns properties to false.

Styling the Validation Error Indicator

In the next chapters, we’ll look into all the cool stuff you can do with the DataGrid, so read on. Now, you’ll observe that when you type in the TextBox, the FlexGrid automatically filters. All you have to do is add a TextBox to your Window and data-bind it to the FlexGrid FullTextFilterBehavior. In WPF, we use XAML to define the UI, which is the best place to define the columns. Once the https://traderoom.info/displaying-data-in-tables-with-wpf-s-datagrid/ columns are defined in XAML, you can easily rearrange, remove, and format each one.

The Standard .NET WPF Datagrid Features

Similarly, I add Freight OrderDate, ShipAddress, and ShipCity fields, that are the names of the columns in our database table. By default, the grid displays all columns available in a DataTable but we can show and hide columns according to our requirement. DataGrid is a powerful control in WPF that allows you to display and edit data in a tabular format. When combined with the Model-View-ViewModel (MVVM) design pattern, you can create robust and maintainable applications. In this tutorial, we will walk through a comprehensive example of implementing a DataGrid in WPF using C# and MVVM.

  1. Now let’s create a collection of Author objects by using the List class.
  2. UI automation enables script-based testing and assistive technology.
  3. When combined with the Model-View-ViewModel (MVVM) design pattern, you can create robust and maintainable applications.
  4. To receive a license key, visit xceed.com and download the product, or contact us directly at and we will provide you with a trial key.
  5. The FrozenColumnCount property represents the number of columns that user can not scroll horizontally.
  6. In WPF, we use XAML to define the UI, which is the best place to define the columns.
  7. For this example, and the others in this article, I am using the ubiquitous Northwind database.

Constantly evolving—no other datagrid is updated as often—it has more features than any other offering and a flexible, extensible object model. It also provides unbeatable performance by handling millions of rows and thousands of columns, and integrates easily into any WPF app. The DataGridCollectionView and DataGridDetailDescription classes support automatic filtering, which provides Excel-like end-user filtering according to the distinct values of each column. The DistinctValuesConstraint property can also be set to determine if the distinct values are to be filtered according to the result of previous auto-filtering operations. If you’re developing Windows desktop applications, you may be able to get by with simple datagrids using the native .NET library.

Data binding

The HeaderVisibility property is used to show and hide row and column headers. The data loaded in DataGrid looks like Figure 2, which shows the properties of the Author class a column names. Figure 1 shows Toolbox and XAML code preview after a DataGrid is added to a page. Supports multi-level grouping with all the related features, as well as master-detail hierarchy with a single-scrollbar approach for the cleanest user experience. To receive a license key, visit xceed.com and download the product, or contact us directly at and we will provide you with a trial key. Open the solution file in Visual Studio and build the project to restore the necessary NuGet packages.

Note that we are still binding the DataGrid’s ItemsSource to the inherited DataContext. The SelectionMode property decides if the DataGrid allows only a single row or multiple rows selection. The following code snippet sets column width and row height to 100 and 30 respectively.

For Windows Forms, you will define them either in the Visual Studio designer or in C# code. In all .NET frameworks, you can also create and rearrange columns in C# code, so step #3 has some variance based on each framework and your preferences. The standard validation error indicator is a red exclamation mark which is displayed at the left hand side of the row.