Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Windows Forms
Article

Extending the DataGridView

Rate me:
Please Sign up or sign in to vote.
4.65/5 (33 votes)
23 Dec 2008CPOL6 min read 198K   6.9K   174   41
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)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Moumit Mondal20-Aug-12 21:30
Moumit Mondal20-Aug-12 21:30 
GeneralIssues with Visual Studio 2010 Pin
PhooManChew10-Jan-11 5:24
PhooManChew10-Jan-11 5:24 
GeneralRe: Issues with Visual Studio 2010 Pin
Bobo7869-Feb-15 20:26
Bobo7869-Feb-15 20:26 
Generalan improvement Pin
xisket19-Sep-10 14:01
xisket19-Sep-10 14:01 
GeneralKewDown on Enter Pin
lalalav11-Jul-10 22:43
lalalav11-Jul-10 22:43 
QuestionSorting issue Pin
gwired30-Dec-08 5:59
gwired30-Dec-08 5:59 
AnswerRe: Sorting issue Pin
Chris_McGrath30-Dec-08 11:19
Chris_McGrath30-Dec-08 11:19 
GeneralCompile error [modified] Pin
ArielR29-Dec-08 23:45
ArielR29-Dec-08 23:45 
GeneralRe: Compile error Pin
Chris_McGrath30-Dec-08 11:03
Chris_McGrath30-Dec-08 11:03 
GeneralRe: Compile error Pin
ArielR31-Dec-08 0:47
ArielR31-Dec-08 0:47 
NewsAn update will happen soon. Pin
Chris_McGrath19-Dec-08 22:47
Chris_McGrath19-Dec-08 22:47 
NewsRe: An update will happen soon. Pin
Chris_McGrath20-Dec-08 13:06
Chris_McGrath20-Dec-08 13:06 
Questionnice article, but plz explain how can we do following Pin
sairfan121-Nov-08 22:05
sairfan121-Nov-08 22:05 
AnswerRe: nice article, but plz explain how can we do following Pin
Chris_McGrath23-Nov-08 12:43
Chris_McGrath23-Nov-08 12:43 
Generalto make datgridview act as edit menu Pin
manojsakthikumar18-Jul-08 1:32
manojsakthikumar18-Jul-08 1:32 
GeneralRe: to make datgridview act as edit menu Pin
Chris_McGrath20-Jul-08 13:24
Chris_McGrath20-Jul-08 13:24 
GeneralSearch feature question Pin
ajliaks30-Jun-08 22:03
ajliaks30-Jun-08 22:03 
Generalsummarise perticular datagrid column( get sum of datagrid column data) Pin
ayeshika27-Jun-08 7:56
ayeshika27-Jun-08 7:56 
GeneralRe: summarise perticular datagrid column( get sum of datagrid column data) Pin
Chris_McGrath29-Jun-08 13:47
Chris_McGrath29-Jun-08 13:47 
QuestionWill it support Both IE and firefox? Pin
Srinath Gopinath24-Jun-08 0:05
Srinath Gopinath24-Jun-08 0:05 
AnswerRe: Will it support Both IE and firefox? Pin
Chris_McGrath24-Jun-08 13:23
Chris_McGrath24-Jun-08 13:23 
QuestionExcellent article Pin
ajliaks22-Jun-08 6:24
ajliaks22-Jun-08 6:24 
AnswerRe: Excellent article Pin
Chris_McGrath22-Jun-08 13:27
Chris_McGrath22-Jun-08 13:27 
GeneralRe: Excellent article Pin
ajliaks22-Jun-08 19:50
ajliaks22-Jun-08 19:50 
GeneralRe: Excellent article Pin
Chris_McGrath22-Jun-08 19:53
Chris_McGrath22-Jun-08 19:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.