DEV Community

Cover image for The Definitive Guide to C# .NET Datagrids
Chelsea Devereaux for MESCIUS inc.

Posted on • Originally published at Medium

The Definitive Guide to C# .NET Datagrids

A .NET datagrid is a user interface (UI) control for displaying bound data in a tabular format. They are powerful controls that provide many productivity and data analysis features for .NET applications. A datagrid is similar to the HTML Table but has added features like column sorting, column resizing, and built-in cell editing. .NET datagrid applications are typically written in C# but may also support VB.NET.

In this article, we share the evolution of the datagrid for developing .NET applications from the Windows desktop to the web, the top features you’ll find in a datagrid, common development scenarios for C# datagrids, and more.

The Evolution of C# .NET DataGrids

From its humble origins, the .NET datagrid has transformed into a versatile yet complex software component because of technological advancements and evolving business requirements. Because of its complexity, you must peel back the layers to better understand the control.

WinForms Datagrids

The first .NET DataGrid control, released by Microsoft with .NET 1.0 for Windows Forms and bundled with Visual Studio, took tabular data and displayed it onscreen in the form of rows and columns. It had basic designer configuration support and included paging, sorting, and updating support, all of which required writing code.

With .NET 2.0, Microsoft wrote a new datagrid control named DataGridView. This control added enhanced design-time capabilities, new data-binding features, and out-of-the-box sorting and paging features. Microsoft defined more run-time events, also known as “callbacks,” to extend the behavior and adjust the look and feel of the grid at runtime.

Windows Forms DataGridView Control

ASP.NET Datagrids

A second version of the DataGrid control, now named GridView, was also developed for web applications written with ASP.NET. Web applications originally didn’t have the full power of native desktop applications, so inline editing was not easily feasible. Editing was limited to one row at a time, typically with an edit button column that would transform each row into several textboxes and submit all edits as a batch response to the server. These runtime features on the client were continuously improved over time with the help of JavaScript and jQuery.

ASP.NET AJAX C1GridView

WPF Datagrids

With .NET 3.0, Microsoft included a new DataGrid control for its new presentation layer, WPF. One might speculate that WPF (Windows Presentation Foundation) was intended to replace Windows Forms; however, both are built upon the same Win32 libraries and are still widely used today. The WPF DataGrid offered a slightly different feature set and limits than the Windows Forms version. You’ll find a comparison later in this article.

WPF DataGrid Control

.NET Datagrids for Newer Frameworks

The DataGridView in WinForms and DataGrid in WPF are still supported and used today with .NET 8.0 (and soon .NET 9.0) applications. There is a DataGrid provided for UWP (WinUI 2), but it’s not currently available for WinUI 3 or .NET MAUI.

For ASP.NET technologies, we have shifted to ASP.NET Core, Razor Pages, Blazor, and MVC. Surprisingly, these newer web frameworks do not have any native .NET datagrid control. The reason is that these libraries heavily depend on JavaScript more than server-side C# code, so the .NET team has left it up to developers to acquire their own UI libraries.

Since these newer web frameworks lack native controls aside from pure HTML and JavaScript, you will want to use powerful third-party datagrid controls that replicate the same components in the earlier frameworks.

Third-Party .NET Datagrids Take Off

From the start, developers ran into limitations with the standard .NET controls. They received requests from their users to add more features made popular by other applications like Microsoft Office, or, as explained, some newer .NET frameworks like Blazor lacked powerful UI controls. Most developers didn’t want to write colossal amounts of code to implement these advanced datagrid features, but where there’s a problem that needs solving, there are companies willing to sell you their solutions.

These problems gave rise to the third-party component ecosystem, which Microsoft heartily supported because it ultimately added value to their development environment, Visual Studio.

This plight resulted in .NET component vendors adding the features that developers found lacking in the default Microsoft controls, including:

  • Multi-level grouping for hierarchical display
  • Multi-column sorting to make fundamental analysis easier
  • Auto-sized columns and rows–for responsive applications
  • Rich design-time support for configuring appearance and behavior without writing any code
  • Advanced cell customization for editing and visualizing unique data
  • Unbound columns for more accessible, dynamic data display
  • Merge/split cells and rows, similar to Microsoft Word and Excel
  • Flexible styling options to apply custom branding to the user interface
  • Freezing and pinning columns, similar to Microsoft Excel
  • Custom printing and export to popular formats like Excel, CSV, HTML, and PDF

And, of course, even basic datagrid controls for newer web frameworks in addition to WinUI and .NET MAUI.

For example, the ComponentOne FlexGrid is a cross-platform .NET datagrid supported in ASP.NET MVC, Blazor, Windows Forms, WPF, UWP, WinUI, Xamarin, .NET MAUI, and even pure JavaScript. FlexGrid is an ideal choice for developers and enterprise companies. It supports the same production-ready features as the .NET datagrid but also elevates your application to the next level with custom cells, on-demand loading, built-in filtering, and file export. ComponentOne products are provided by Mescius.

ComponentOne FlexGrid Datagrid Control

Those are just some of the feature highlights. Next, we’ll look at the full feature set for a typical C# .NET datagrid and how the features compare across frameworks and third-party datagrids.

The Top Features of a C# .NET Datagrid

The primary uses of .NET datagrids include features for displaying, editing, and analyzing data. Let’s break down some key features in these areas across each .NET platform.

.NET Datagrid Display Features

Display features help the user read and understand the data more quickly and efficiently. You can improve the readability of the raw data with cell formatting, merging, and column bands. The table below shows how the top (built-in) display features stack up against a third-party datagrid, like FlexGrid.

.NET Datagrid Display Features

.NET Datagrid Editing Features

Datagrids are primarily designed for editing, as you can get a quick, barebones table editor by simply databinding. The standard .NET datagrids provide basic features and extensibility but do not have as many built-in features as third-party datagrids.

.NET Datagrid Editing Features

WinForms Datagrid Analysis Features

The standard .NET datagrids are designed primarily for displaying and editing. If you’re looking for more advanced features that fall into the analysis category, you’ll find more of these features built into third-party datagrids, as you can see below. A built-in feature typically means that it’s enabled with very little code — often by setting just one property.

WinForms Datagrid Analysis Features

A missing check means it’s not a built-in runtime feature, but it may still be possible by writing some code.

An exception is that, by design, many WPF DataGrid features require custom converters or styles or rely on configuring the CollectionViewSource. While these may not be “built-in” the same way as WinForms, they are fairly easy to use, considering the nature of the platform.

Some ASP.NET features considered “built-in” require minimal JavaScript, such as a format function.

The trade-offs to getting more built-in features with a third-party datagrid are extra steps to acquire the libraries and additional costs associated with licensing the control. Next, let’s look at how to build and use a datagrid control.

How to Build a C# .NET Datagrid

Let’s look at three common approaches to working with a .NET datagrid, from quick and simple to advanced customization.

Fast Databinding Scenario (Easy)

In the simplest scenario, we simply data-bind and build! Most datagrid controls automatically generate columns for each field and come with basic editing and sorting features out of the box. In many simple cases, this is all you need, but notice that every column will display in the exact order in which it is discovered in the database.

Bound DataGrid with Auto-Generated Columns

The exact steps can vary for each .NET framework. The basic steps are:

  1. Instantiate the datagrid control
  2. Set the datagrid’s datasource to your data collection
  3. Build your application

As you can probably tell, the most complex part of this scenario is just obtaining your data collection. The data collection could come from a web service, SQL Server, JSON file, or anywhere. UI controls are generally data agnostic and do not care where the data comes from, but you will need to convert the data to some collection type that the datagrid control recognizes. In .NET, there is a large array (pun intended) of data types that can be used with datagrids, including List, CollectionView, and ObservableCollection.

A short C# code example for Windows Forms is below:

// obtain data set
List<Customer> customers = new List<Customer>();
// populate data set (omitted)
dataGridView1.DataSource = customers;
Enter fullscreen mode Exit fullscreen mode

For coded examples using Windows Forms, check out our previous article, The Definitive Guide to WinForms Datagrids.

Column Configuration Scenario (Moderate)

By default, datagrids will automatically generate all the columns based on your data source. 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. So, in most cases, you will need to perform some slight modifications to the columns, such as reordering and formatting.

Blazor FlexGrid with Formatted Columns

Columns are rarely in the “perfect” order from the data source, so you will likely need to customize them completely to achieve your desired order. Most datagrid controls also have a quick and easy way to format column text for text and date values once you have defined the columns in code or markup (for example, “c” is the format string for currency).

The general steps for this scenario are:

  1. Instantiate the datagrid control
  2. Disable the datagrid’s automatic column generation
  3. Define each column you want to be displayed in your desired order
  4. Apply simple column formatting for dates & numbers
  5. Set the datagrid’s datasource to your data collection
  6. Build your application

In XAML and HTML frameworks, you will typically define the columns in the markup directly within your datagrid tags. 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.

Below is a short C# code example for Windows Forms:

// disable automatic column generation
dataGridView1.AutoGenerateColumns = false;

// define each column in order, with optional format
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "Name";
col1.HeaderText = "Customer name";
col1.DefaultCellStyle.Format = "c";

// add column to datagrid
dataGridView1.Columns.Add(col1);
Enter fullscreen mode Exit fullscreen mode

After you’ve customized your displayed columns, you still receive all the same editing and sorting features out of the box.

Complete Datagrid Customization (Advanced)

If you need more than the built-in editing, sorting, and moderately easy column customization, then you are likely looking at an advanced datagrid customization. These advanced features include:

  • Grouping by columns
  • Editing with custom cell editors
  • Displaying custom objects in cells
  • Create hierarchical grids with collapsible rows and drill-down details
  • Filtering by drop-down menus or a filter row
  • Freezing columns and rows in place during scroll

Some of these features are possible with the standard datagrids but require writing a lot of code. If you use a third-party datagrid, you can enable most of these features rather quickly with just a few lines of code.

Advanced GanttView Created from FlexGrid for WinForms

What about import and export?

We’ve reviewed displaying and editing, but you may be wondering about import and export. Regardless of what datagrid or .NET framework you use, these features will require additional C# or VB.NET code. Datagrid controls typically have a built-in feature for adding new rows, which can be enabled by setting one property; however, if you need to import/export from a file, such as a Microsoft Excel file, you will need an additional library to help.

Third-party datagrids, such as FlexGrid, typically help you in this area with auxiliary libraries that include the Excel (or Word, CSV, PDF, etc.) formatting libraries along with an extended “Export” method.

.NET Datagrids with Spreadsheet-like Features

Microsoft Excel is the most well-known and used spreadsheet application in the world. Even its competitors offer similar features. If you’re looking for even more Excel-like features for your .NET datagrid, you may want to consider a specialized spreadsheet component.

When designing reusable component libraries, there is a limit to the number of features you should add because nobody wants application bloat (where only a small percentage of the features are used). The solution is to break out specialized features into separate libraries. For .NET datagrids, this includes specialized grids that look and feel like Microsoft Excel, as well as Microsoft Project (GanttViews) and Power BI (Pivot Tables).

The Excel-like spreadsheet features, which added support for exporting and printing–much like other reporting tools — also include:

  • A spreadsheet-style look and feel to leverage the popularity of Microsoft Excel
  • Advanced row filtering to narrow down results, also inspired by Excel
  • Embedded input controls within cells like text and images
  • Multi-line rows to display composite information, such as an address, within a single cell
  • Dynamic cell drawing for complete control of the grid’s appearance
  • Virtual data scrolling to display large amounts of data while providing a seamless user experience
  • Configurable views, the next version of styling options
  • Support for a range of export formats, including images, Excel, Word, and PDF

PivotTables

MESCIUS Spread.NET is the number one best-selling spreadsheet component for .NET.

Conclusion

Over time, we’ve moved from basic .NET datagrids provided by the framework to requiring more advanced features and solutions across a much wider span of frameworks. Open-source and third-party component vendors have filled in the gaps left open by the core .NET framework. If you consider the costs of developing the features yourself versus buying off-the-shelf, you’ll often find that third-party libraries pay for themselves for the saved time and resources.

If you’re developing Windows desktop applications, you may be able to get by with simple datagrids using the native .NET library. Still, if you need advanced features or themes, or if you’re developing for the web, you will definitely want to add a control suite to your toolbox since the only controls available are basic HTML elements.

Top comments (0)