Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Extending the DataGridView

4.65/5 (33 votes)
23 Dec 2008CPOL6 min read 3   6.9K  
A few minor modifications to improve the DataGridView.

Introduction

The DataGridView is a powerful control, but there are some features missing. Some of these features include: a quick way to search through the data, ability to show/hide columns, export data, and keyboard shortcuts.

Searching

I wanted a Search feature which is very simple to use. I also wanted it to search as you typed. So, I took my inspiration from Firefox. A small bar at the bottom with one field to type in. The logic is quite simple. Search the sorted field for the text. To bring up the search, choose it from the header context menu, or press ` (above the Tab key).

Show/Hide Columns

When I display data to the user, there will quite often be advanced data that the user should be able to view, but hidden by default. So, with my control, you can simply click on the header to get the option to show and hide columns. If you don't want the user to be able to do this for a particular grid, simply use:

C#
AllowAddRemoveColumns = false;

Export Data

A grid is much more useful if it can be exported into Excel. I have used the Microsoft Office 2003 Primary Interop Assemblies (must be installed on the client), which will import to Office 2003 and 2007. If it is not installed, the option will be changed to Export to CSV. A problem with Microsoft Office 2003 Primary Interop Assemblies is that Office must be installed to build the solution. To fix this, I use a fair bit of Reflection, but it is all in wrapper classes to keep the code neat.

This option can be found in the header's context menu.

Keyboard Shortcuts

The ability to sort and show/hide columns would be more useful to power users if they can do it from their keyboard. But I also want it to be intuitive. So, when you press the Control key the column numbers appear - these will be what we use for the shortcuts. Control + Column Number will sort on that column, and Control + Alt + Column Number will show/hide the column. The other important shortcut is, as mentioned above, the ` key (above the Tab, shift value is ~) for the Search.

Column Editor

This is a feature added in version 1.0.2. Perhaps the most annoying part of the normal DataGridView is it's column editor. Most of it is quite good. The problem is that the dialog is too small and you have to resize it. Then you close it, open it again and have to resize it again. It's only a small thing but it's the thing that annoys me more than anything in Visual Studio (and I'm not alone!). Since Microsoft hasn't fixed this (and may I point out it would take them less than 10 minutes to fix) I decided to fix it myself.

The Changes to the ExtendedDataGridView are minimal - You just require two attributes the first on the class of [DesignerAttribute(typeof(ExtendedDataGridViewDesigner))] and the second on the Columns property of [Editor(typeof(ExtendedDataGridViewColumnCollectionEditor), typeof(UITypeEditor))]. The classes these Attributes reference is where most of the work is done.

The ExtendedDataGridViewColumnCollectionEditor is the most important. The code is very similar (and based off of) the DataGridViewColumnCollectionEditor in System.Design. The main problem is that we require access to classes in that assembly which are marked as internal (the DataGridViewColumnCollectionDialog). So we must use reflection to get it. DataGridViewColumnCollectionDialog inherits from Form so when we get the object we will be working on it as a Form - which is all we need. We can easily set the MaximizeBox property to true and it's easy to get and set the Location and Size.

There is one further trick though, there is a small bug in DataGridViewColumnCollectionDialog. Normally it's hardly noticable but when we give it a larger initial size it's quite annoying. The problem is that it doesn't shrink from the initial size well. It grows well and shrinks to a size >= to the initial size well but when it gets smaller it acts like all controls are aligned to Top Right and Controls (Ok, Cancel and part of the Grid) are off screen. The easiest solution is to start with the form as small as possible and on the OnLoad set it to the Size we want.

And that's it! we have fixed the column editor! Well, almost. that fixes the Column Editor when you go to it from the Property Explorer. It doesn't fix it for when you click the thing on its top right coner and chose edit columns from the task pane. That's where ExtendedDataGridViewDesigner comes in. I won't go into much detail as it's mainly just copying the DataGridViewDesigner (from System.Design) code. Unfortunately I had to copy and not just inherit or use reflection to get most of the work done for me. But one advantage of this is if I want to add some of my own stuff to the task pane later I can.

So there we have it! A fully integrated improved Column Editor which is less annoying and should hopefully save you some time (even if it's just the time saved in not ranting about how annoying the size of the column editor is).

Other Features

How many times have you tried to figure out what the size of columns should be? The designer only lets you enter a number, see if it looks good, fix it up, check that out, and repeat until you are happy. On my control, if you press Shift while opening the header context menu, it displays the width next to each column. So, you can visually change the column width and then find out how wide it is.

Finally enums, my project includes a DataGridViewColumn for giving enums good descriptions. All you have to do is when you make an enum, declare it as follows...

C#
public enum Example
{
    [Description("Enum Example 1")
    EnumEx1,
    [Description("Enum Example 2")
    EnumEx2,
}

History

  • Version 1 - Initial project.
  • Version 1.0.1 - Fixed a bug which stops some keyboard shortcuts from working when the header menu is still to be shown.
  • Version 1.0.2 - Project is now for Visual Studio 2008 (Sorry people on VS2005 but I don't have it installed anymore). The Column Editor now has a maximise button and remembers its location and size.

Final Thoughts

I would just like to thank everyone who has read this article and especially those who left feedback. I'm glad so many people have found it useful.

I noticed that some people rated it badly. I don't mind but if you do rate it badly please leave some feedback explaining why. Giving a bad rating doesn't improve an article. But leaving feedback explain why does.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)