Skip to main content Skip to footer

Where to Download C# .NET WPF Datagrid Examples

Quick Start Guide
What You Will Need

Visual Studio

.NET 8

Controls Referenced

FlexGrid for WPF

Tutorial Concept Learn where to find specific WPF datagrid samples for different features and use cases.

A DataGrid control enables you to display and edit data from many different sources, such as a SQL database or business object collection. The Microsoft .NET DataGrid documentation and Data Binding samples are a great starting point, but they do not cover every possible feature you need, and many of the resources you’ll find are for .NET Framework only.

In this article, we’ll show off several specific datagrid features that you may need and highlight where to find additional samples for C# .NET 8+ development. These samples use the WPF FlexGrid control, which is a more powerful WPF DataGrid that enables easier customization.

WPF datagrid examples covered in this article include:

Ready to Get Started? Download ComponentOne Today!

Animated Datagrid Rows

The Custom Rows WPF datagrid sample demonstrates an animated row style that would be useful for a dynamic dashboard such as a stock ticker. This datagrid row style is also modeled to look like a modern List View. This datagrid style is commonly used in mobile displays where we focus more on vertical scrolling and less on horizontal scrolling.

Custom Rows

The feature is specially enhanced for FlexGrid thanks to its CellFactory class. It’s kind of like a special WPF Converter for datagrid cells. The CellFactory converts the raw underlying data value into the cell presentation for both display and editing use cases. This particular example demonstrates how to create a unique look with left, right, and center cell concepts. Below is a C# and XAML code snippet.

grid.CellFactory = Resources["listViewCellFactoryStyle"] as GridCellFactory;

...

<local:ListViewCellFactoryStyle x:Key="listViewCellFactoryStyle">
    <local:ListViewCellFactoryStyle.LeftCellStyle>
        <Style TargetType="c1:GridCellView" BasedOn="{StaticResource cellStyle}">
            <Setter Property="BorderThickness" Value="1,1,0,1"/>
            <Setter Property="CornerRadius" Value="10,0,0,10"/>
            <Setter Property="Margin" Value="8,4,0,4"/>
        </Style>
    </local:ListViewCellFactoryStyle.LeftCellStyle>
    <local:ListViewCellFactoryStyle.CenterCellStyle>
        <Style TargetType="c1:GridCellView" BasedOn="{StaticResource cellStyle}">
            <Setter Property="BorderThickness" Value="0,1,0,1"/>
            <Setter Property="CornerRadius" Value="0,0,0,0"/>
            <Setter Property="Margin" Value="0,4,0,4"/>
        </Style>
    </local:ListViewCellFactoryStyle.CenterCellStyle>
    <local:ListViewCellFactoryStyle.RightCellStyle>
        <Style TargetType="c1:GridCellView" BasedOn="{StaticResource cellStyle}">
            <Setter Property="BorderThickness" Value="0,1,1,1"/>
            <Setter Property="CornerRadius" Value="0,10,10,0"/>
            <Setter Property="Margin" Value="0,4,8,4"/>
        </Style>
    </local:ListViewCellFactoryStyle.RightCellStyle>
</local:ListViewCellFactoryStyle>

Download the complete Custom Rows FlexGrid sample here on GitHub.

Dynamic Data Binding with ExpandoObject

The ExpandoObject was introduced in C# .NET 4.0, and it helps you create complex hierarchical objects at runtime. It also implements the INotifyPropertyChanged interface, which gives you more control over properties than a dictionary.

The Dynamic Binding sample shows how to bind and edit a list of dynamic ExpandoObjects to a WPF datagrid using FlexGrid; the resulting code is the same as if you were using any business object!

var customers = new ObservableCollection<System.Dynamic.ExpandoObject>();

dynamic employee1 = new System.Dynamic.ExpandoObject();
employee1.Name = "John";
employee1.Age = 18;

...

customers.Add(employee1);
...

grid.ItemsSource = customers;

Download the complete Dynamic Binding WPF datagrid example here on GitHub.

Hierarchical Datagrid Rows with Outlining

The Hierarchical Rows sample shows how to display a hierarchical structure in FlexGrid with collapsible buttons in the row headers. This feature is great for visualizing outlines and tree-structured data, complete with custom outline and row numbering.

HierarchicalRows

The sample also demonstrates how to display summarized information on the group node rows, such as the total duration of tasks. Download the Hierarchical Rows sample here on GitHub.

WPF Datagrid Printing Sample with Print Preview

The FlexGrid Printing sample demonstrates how to print a WPF datagrid using FlexGrid. The sample also includes print preview and page setup (margins, orientation, scaling). Plus, it shows how you can customize certain features to be included in the printed output, such as cell merging and freezing.

Printing

The way this works is that the FlexGrid has a Print method that prints the grid. The Print method allows you to specify basic printing parameters, such as document title, margins, and scaling mode.

If you need more flexibility, the FlexGrid also provides a GetPageImages method that returns a list of images that represent a paged view of the grid. These images can then be added to pages in a PrintDocument to print the grid by itself or in combination with other elements. The GetPageImages method truly makes the FlexGrid flexible.

Download the complete WPF datagrid Printing sample here on GitHub.

SQLite Data Binding with Entity Framework

The SQLite Database sample demonstrates data binding a SQLite database to a WPF datagrid control using Entity Framework Core. It shows real-world performance for 100,000 rows with server-fetched filtering.

The datagrid can be bound to an Entity Framework Core source through the C1EntityFrameworkCoreVirtualDataCollection and C1EntityFrameworkCoreCursorDataCollection from the C1.DataCollection.EntityFrameworkCore NuGet package.

var cv = new C1EntityFrameworkCoreVirtualDataCollection<Person>(db);
grid.ItemsSource = cv;

SQLite

Download the complete SQLite Database sample here on GitHub.

WPF Datagrid Examples Summary

You can find all the samples above and more in the ComponentOne WPF GitHub repo. Some of the code also works with the .NET DataGrid control; however, most require the additional benefits of FlexGrid to work fully.

Ready to try it out? Download ComponentOne Today!

For more C# code examples of common datagrid features, check out the FlexGridExplorer sample on GitHub. This sample is the best starting point for learning how to develop with FlexGrid.

If you’re still working with the standard DataGrid control, the Microsoft .NET documentation is a good place to learn how to implement basic features in your WPF application. If you want to see some features in action, you can try the WPF Gallery application. Finally, if you’re looking for C# code examples, you’ll find them in the WPF Samples Github repo.

Tags:

comments powered by Disqus