WPF DataGrid Lightning Fast Grid Control

wpf advanced datagrid

DataGrid ColumnsEarlier in this book, we saw when the AutoGenrateColumns property is set to true, the DataGrid automatically generates columns based on the data source and its objects and public properties. However, in most of the applications, we define columns, their types and bind to data source properties. The Columns property is a collection and provides methods to add, update and delete columns from the collection. To create manual columns, the AutoGenerateColumns property must be set to false.

Article Overview

Either layout can be used to display data items as cards; however, the compact card-view layout applies well when a database has many columns but few of the fields contain data. In this case, the fields that do not contain data will not be displayed in the cards, giving them a more compact appearance. The WPF DataGrid control is used for efficiently displaying and manipulating tabular data. Its rich feature set includes functionalities like data binding, editing, sorting, filtering, grouping, and exporting to Excel and PDF file formats. It has also been optimized for working with millions of records, as well as handling high-frequency, real-time updates.

Industry’s Best WPF Datagrid Control

Since it’s a lot easier to demonstrate, we’ll mostly be using the latter approach in this tutorial. Assign users (collection) as a ItemsSource of DataGrid to display the data on datagrid. Serialize the WPF DataGrid settings to XML format and load back (deserialization) https://traderoom.info/ to a data grid using the built-in serialization options. Users can change the visibility of columns at runtime using the customizable column chooser UI. Users can freeze rows and columns at the top, bottom, left, and right positions, similar to in Excel.

Code of Conduct

The DataGrid is also editable by default, allowing the end-user to change the values of the underlying data source. In the markup, I have defined a couple of columns and then I use the RowDetailsTemplate to specify a template for the row details. This example is a simple CRUD application which allows the user to edit items in the Customers table of the Northwind database. Because we are only performing CRUD functions, I have not added a Business Logic Layer (BLL); if you are a purist, you could add a pass-through BLL; however, I feel it would add little to this example. When the user edits the Customers data within the DataGrid, the bound in-memory DataTable is updated accordingly.

This article will present a few common validation scenarios, demonstrating how the DataGrid can be configured to perform these tasks. Note that the examples I give all delegate the validation logic to the bound object itself, rather than having the rules which dictate whether an object state is valid or not live entirely within the UI. Both are, of course, possible; however, it is my preference that the validation logic should not live in the presentation layer. With the above code, the design-time support of the DataGrid is now available, allowing you to configure which columns are present, their bindings, sorting, etc. For this example, and the others in this article, I am using the ubiquitous Northwind database. The DataSet design for these examples was created via simple drag and drop from the Server Explorer.

wpf advanced datagrid

Row drag and drop

In order to make the failure more obvious, the style of the row has been modified to add a red border. This rule iterates over all of the items within the binding group (i.e., the DataGrid row) probing them for errors. In this case, the IDataErrorInfo.Error property is used for object level validation. Eventually, in August 2008, Microsoft released its DataGrid CTP (Community Technology Preview – a public beta) to CodePlex to coincide with the release of the .NET Framework 3.5 SP1 and Visual Studio 2008 SP1. The .NET Service Packs provided additional WPF functions including IEditableCollectionView, BindingGroup, and alternating row styles which are all used within the WPF DataGrid. Another small yet very highly welcome feature of SP1 is the Binding.StringFormat which provides a simple mechanism for formatting bound data, as described in this blog post.

The mission of MESCIUS is to provide customers with the premier studio of innovative UI controls, tools, and solutions including deep functionality reporting, analysis, and spreadsheet platforms. This element is used to render the sort arrows, and optionally render a thin separator between each header if SeparatorVisibility is set to Visible. This rendering takes place within the OnRender method of this class; however, if a Background or BorderBrush is set, the rendering is performed by the standard WPF Border class; therefore, arrows and separators are lost. The style of the column header can easily be modified via the ColumnHeaderStyle of the DataGrid. However, if you modify the background colour of the column header, you will find that the sort arrows disappear! This is because the arrows are not part of the ColumnHeader template; instead, they are added programmatically.

  1. I wrote extensively about Avalonia on CodeProject and produced several Avalonia based packages/frameworks also described in my CodeProject articles.
  2. The presentation as a checkbox depends on the field type, DateTime for the TreeView and all other types for the ListBox.
  3. It’s also possible to place a newly added row at the top or bottom in a DataGrid.
  4. As seen by the example of this article, this opens up for some pretty interesting possibilities, where you can completely customize the editor and thereby enhance the end-user experience.

Most of the rest of the code is defined within MainWindow.axaml file, only references to style files are defined within App.axaml. Items Checked remain visible in the DataGrid, the raw value of other items is stored in the list PreviouslyFilteredItems of each filter, this operation is carried out in the method ApplyFilterCommand when the Ok button is clicked. If the name of the field is equal to the name of the last filtered field, the already filtered values of this field are added to this list. These two types of columns have been extended with their base class and two DependencyProperty have been implemented, IsColumnFiltered and FieldName, see the file DataGridColumn.cs. Note also that the read-only view of this cell uses the StringFormat property of the Binding class to specify a format which matches that which is used by the DatePicker.

Display rows at the top and bottom of the grid with data that is not bound to the ItemsSource. It’s also possible to place a newly added row at the top or bottom in a DataGrid. The WPF DataGrid control can handle high-frequency updates even under the most demanding scenarios where the data is sorted and grouped in real-time. The summaries are calculated in an optimized way and the updated summaries are displayed in real-time scenarios. Group data by one or more columns either through mouse and touch interactivity in the group drop area or in code behind.

wpf advanced datagrid

The workaround given here uses validation to ensure that all the DataTable constraints are satisfied before the updates are committed. The following slightly lengthy validation rule can be applied at cell and row level. The rule will check a column (or all the row’s columns) for length, unique, and null constraints.

Being able to show details for a DataGrid row is extremely useful, and with the WPF DataGrid it’s both easy and highly customizable, as you can see from the examples provided in this tutorial. Improve performance and reduce load times for very large data sets with on-demand loading virtual mode, where data is only retrieved as the user scrolls, or paging, where data is loaded in segments. One of the main strengths of FlexGrid is the ability to customize every aspect of the appearance of the entire grid and individual cells. This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community.For more information see the .NET Foundation Code of Conduct. The final example is just a demonstration of the type of styling that can be applied to a WPF DataGrid. We have all the usual suspects, rounded glass effect tabs for column headings, grouping with a customised Expander template, mouse-over highlighting, etc.

The appearance of a data grid and its inner elements, such as rows, cells, columns, headers, and summary rows, can be customized easily using styles and templates. The columns that are displayed in a grid can be chosen by the user through the column-chooser context menu, which can be enabled by setting the AllowColumnChooser defined on the view to true. A column’s ShowInColumnChooser property determines whether a column’s title is displayed in the menu, allowing its visibility to be manipulated by an end user. The DataGrid control looks a lot like the ListView, when using a GridView, but it offers a lot of additional functionality. For instance, the DataGrid can automatically generate columns, depending on the data you feed it with.

It is up to the developer to decide when changes to the DataTable are written back to the database depending on the requirements of the application. For example, in some cases, you might wish to submit a batch of changes via a “Submit” button, or you may wish to have the database updated as the user commits each row edit. In order to support https://traderoom.info/displaying-data-in-tables-with-wpf-s-datagrid/ these, the rows that the DataTable contains have a RowState property which indicates whether they contain changes which should be synchronized with the database. The synchronization process is easily achieved via the TableAdapter’s Update method. The CardView and CompactCardView classes provide card-view layouts for the data items in a grid.

DataGrid supports right-to-left (RTL) rendering for users working in right-to-left languages like Hebrew, Arabic, or Persian. Data processing operations like sorting, filtering, grouping, summarizing, and real-time updating are handled efficiently using PLINQ. The WPF DataGrid loads millions of records in just a second without any performance degradation with the help of row and column virtualization.

Avatar for Abhishek

Abhishek