New developers often struggle with the .NET DataGrid when trying to replicate grid functionality from other platforms, while more experienced developers lament the deficiencies of the .NET DataGrid to address end user requirements. These tips will help both newbies and proficient developers work with the DataGrid.

by Kevin S. Goff

Dec 20, 2004

Page 1 of 6

any developers seek sophisticated third-party alternatives. In this article, I'll present a set of classes for the DataGrid to help address some of the more common struggles. Although third-party tools always offer more capabilities than a native control, this article also demonstrates how it's possible for you to implement some of the functions found in these third-party tools.
Beginning with the End in Mind
In any development endeavor, one of the key steps is to identify the requirements. I often use the phrase "Begin with the end in mind." It's part of a philosophy of visualizing the end-result from 30,000 feet, as well as understanding all the details.

This article provides some code samples to help ease the transition for those new to .NET. As developers, we sometimes seek a fast-track answer. "Just show me a code example, and I'll take it from there."
Specifically, my objective is to demonstrate a set of classes in C# to cover some of the commonly-asked questions and requests concerning grids:

How to populate grid columns with minimal lines of code

How to implement drop-down lists with descriptions from different tables...and how to implement other controls in a grid columnagain, with minimal code

How to implement Find/Find Next text-searches in a gridfor all columns or only for certain columns

How to turn your Find/Find Next search text into a filter

How to perform other types of filters on the contents of a grid

How to bookmark a specific row in a grid by providing a right-click pop-up, so that it can be retrieved later

How to read data from the current column in a grid, even if the grid has been sorted

How to use the binding manager for a grid

How to conditionally set the background color of an individual cell based on a condition

How to set multiple rows in a grid with code, and how to read which rows were selected by a user

How to turn on/off grid row highlighting

How to define a custom function for displaying the caption of a grid

How to save column sizing and other user preferences and retrieve them later

That's a total of thirteen productivity tips, a Baker's Dozen (inspired by the famous Saint Nicholas Cookies from Van Amsterdam's bakery).
Figure 1 shows a test form with a real world scenario that I'll use to demonstrate all of these capabilities.

Figure 1: This runtime form demonstrates some of the DataGrid capabilities.

A few of these capabilities use reflection, and I'll discuss this topic that also raises many questions. I'll also present a reusable function to deal with reflection. Along the way, I'll also touch briefly on other topics, such as inheritance and user controls.
The Development ProjectFigure 2 shows the development solution I'll use to demonstrate grid functionality. It consists of the following four projects:

A test form (TestProjectDataGrid), along with a Form Manager

A library of class controls (ccControls), including the grid class (ccGrid)

A UserControl library (ccContainers), which houses the ccGrid class and tools for searching the grid

A library of reflection functions (ReflectionLibrary) to simplify the task of executing a function whose name is not known until runtime.

For each section, I'll present a scenario where the productivity tip might be needed, how to use the grid and grid container classes to address the scenario, and then how the classes work.
Now, on to the baker's dozen!