Click here to Skip to main content
11,929,675 members (55,510 online)
Click here to Skip to main content
Add your own
alternative version


133 bookmarked

A DataGridView Column Show/Hide Popup

, 23 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A class adding column show/hide capability to a DataGridView


Many applications offer users a useful feature to show/hide columns of data. In Windows Forms, the control dedicated to show tabular data is DataGridView but there isn't a built-in column selection mechanism. I wrote the small class DataGridViewColumnSelector to fill this void.


The DataGridViewColumnSelector class is a new class and not a derivation of the DataGridView class. You don't need to change your DataGridView instances declarations. Simply create an instance of DataGridViewColumnSelector and attach a DataGridView instance to it. When the user right-clicks the cell origin, a pupup is shown, allowing to check/uncheck the columns to show.

The column list is implemented through a CheckedListBox and the popup effect is achieved by means of a ToolStripDropDown object to which a ToolStripControlHost object is added. The latter contains the CheckedListBox. Read the article, "How to: Wrap a Windows Forms Control with ToolStripControlHost", to get some background.

Here is the constructor code:

// The constructor creates an instance of CheckedListBox and ToolStripDropDown.
// the CheckedListBox is hosted by ToolStripControlHost, which in turn is
// added to ToolStripDropDown.
public DataGridViewColumnSelector() {
	mCheckedListBox = new CheckedListBox();
	mCheckedListBox.CheckOnClick = true;
	mCheckedListBox.ItemCheck += 
		new ItemCheckEventHandler(mCheckedListBox_ItemCheck);

	ToolStripControlHost mControlHost = new ToolStripControlHost(mCheckedListBox);
	mControlHost.Padding = Padding.Empty;
	mControlHost.Margin = Padding.Empty;
	mControlHost.AutoSize = false;

	mPopup = new ToolStripDropDown();
	mPopup.Padding = Padding.Empty;

When user right-clicks the cell origin, the mDataGridView_CellMouseClick is called. It clears and fills the CheckedListBox with columns header text. Then it shows the popup. In this way, the CheckedListBox items are always refreshed to reflect changes that occurred in DataGridView columns (column additions or name changes and so on).

void mDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
	if (e.Button == MouseButtons.Right && e.RowIndex==-1 && e.ColumnIndex==-1) {
		foreach (DataGridViewColumn c in mDataGridView.Columns){
			mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
		int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
		mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? 
					PreferredHeight : MaxHeight;
		mCheckedListBox.Width = this.Width;
		mPopup.Show(mDataGridView.PointToScreen(new Point (e.X,e.Y)));

Finally, when user checks/unchecks a checkbox, the related column visibility is switched by mCheckedListBox_ItemCheck event handler.

void mCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e){
	mDataGridView.Columns[e.Index].Visible = (e.NewValue == CheckState.Checked);

Using the Code

Copy the DataGridViewColumnSelector.cs file to your project. Change the namespace if you need.

You can pass the DataGridView instance directly as constructor parameter:

new DataGridViewColumnSelector(dataGridView1);

Or you can create an instance and then attach a DataGridView using the DataGridView property:

DataGridViewColumnSelector cs = new DataGridViewColumnSelector();
cs.DataGridView = dataGridView1;
cs.MaxHeight = 200;
cs.Width = 110;

Optionally use MaxHeight and Width properties to adjust the size of the popup.


  • 23rd December, 2008: Initial post


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


About the Author

Vincenzo Rossi
Software Developer (Senior)
Italy Italy
I'm a graduate in Computer Science.
I work with C++, Visual Basic 6, C#, asp, asp.Net, Windows Forms, SQL Server, Access, Flash.

"Short code, good code"

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Vincent Paukgyi24-Sep-12 23:22
memberVincent Paukgyi24-Sep-12 23:22 
QuestionGood Job Pin
Aliasgar M, Maimoon7-Sep-12 0:54
memberAliasgar M, Maimoon7-Sep-12 0:54 
GeneralMy vote of 5 Pin
ProEnggSoft29-Feb-12 20:21
memberProEnggSoft29-Feb-12 20:21 
Questionthanks your code! Pin
caohit18-Feb-12 4:15
membercaohit18-Feb-12 4:15 
QuestionWhat If I have Columns That Must Stay Hidden? Pin
pointeman130-Dec-11 4:01
memberpointeman130-Dec-11 4:01 
QuestionIt does not work when the datagridview has a contextmenu object Pin
wuhuacong20-Aug-11 18:14
memberwuhuacong20-Aug-11 18:14 
GeneralMy vote of 5 Pin
Member 79087416-Jul-11 10:14
memberMember 79087416-Jul-11 10:14 
GeneralThanks for your info.. Pin
YZK29-Mar-11 23:32
groupYZK29-Mar-11 23:32 
GeneralThanks Pin
YZK29-Mar-11 23:30
groupYZK29-Mar-11 23:30 
GeneralMy vote of 5 Pin
snake2k3y@rambler.ru18-Mar-11 21:57
membersnake2k3y@rambler.ru18-Mar-11 21:57 
GeneralMy vote of 5 Pin
btaz12523-Feb-11 2:18
memberbtaz12523-Feb-11 2:18 
GeneralMy vote of 5 Pin
Wasiq Amjad Bhamla29-Jan-11 10:27
memberWasiq Amjad Bhamla29-Jan-11 10:27 
GeneralMy vote of 5 Pin
sajadjamalian27-Jan-11 21:57
membersajadjamalian27-Jan-11 21:57 
GeneralMy vote of 5 Pin
JunfengGuo5-Dec-10 15:24
memberJunfengGuo5-Dec-10 15:24 
GeneralMy vote of 5 Pin
Member 159022321-Oct-10 17:45
memberMember 159022321-Oct-10 17:45 
GeneralThanks a lot Pin
Sunvory29-Dec-09 16:09
memberSunvory29-Dec-09 16:09 
GeneralPerfect!! Pin
miyayo6-Nov-09 4:48
membermiyayo6-Nov-09 4:48 
GeneralGreat Article Pin
Manvendrak21-Oct-09 18:03
memberManvendrak21-Oct-09 18:03 
GeneralRe: Great Article Pin
Vincenzo Rossi22-Oct-09 9:48
memberVincenzo Rossi22-Oct-09 9:48 
GeneralThank you for the great article. [modified] Pin
Fiwel31-Aug-09 20:33
memberFiwel31-Aug-09 20:33 
GeneralRe: Thank you for the great article. Pin
Fiwel31-Aug-09 20:34
memberFiwel31-Aug-09 20:34 
GeneralRe: Thank you for the great article. Pin
Vincenzo Rossi1-Sep-09 9:46
memberVincenzo Rossi1-Sep-09 9:46 
GeneralRe: Thank you for the great article. Pin
Vincenzo Rossi1-Sep-09 9:57
memberVincenzo Rossi1-Sep-09 9:57 
GeneralRe: Thank you for the great article. Pin
Fiwel1-Sep-09 10:10
memberFiwel1-Sep-09 10:10 
QuestionIs there a VC++ version? Pin
occamRT1-Jul-09 7:55
memberoccamRT1-Jul-09 7:55 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 23 Dec 2008
Article Copyright 2008 by Vincenzo Rossi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid