Click here to Skip to main content
15,172,506 members
Articles / Desktop Programming / Windows Forms
Posted 23 Dec 2008


151 bookmarked

A DataGridView Column Show/Hide Popup

Rate me:
Please Sign up or sign in to vote.
4.80/5 (55 votes)
23 Dec 2008CPOL1 min read
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 <code>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
Technical Lead
Italy Italy
I'm a graduate in Computer Science.
I work with Metatrader MQL4,MQL5 / C# / Asp.Net / Windows Forms / SQL Server / Access / VBA / HTML / CSS / Javascript / classic C/C++.

I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Un'altra estate from the album L'immobile disegno.

"Short code, good code"

Comments and Discussions

QuestionMy Vote is Five Pin
Member 1497935425-Apr-21 5:27
MemberMember 1497935425-Apr-21 5:27 
QuestionGreat Job! Still relevant 10 years on! Pin
Chris J-1378676018-Apr-18 13:02
professionalChris J-1378676018-Apr-18 13:02 version Pin
SumitSaha26-Apr-15 0:19
MemberSumitSaha26-Apr-15 0:19 
AnswerRe: version Pin
Member 1177279214-May-20 1:55
MemberMember 1177279214-May-20 1:55 
QuestionDoing in VB.Net Pin
Member 1135699225-Apr-15 12:40
MemberMember 1135699225-Apr-15 12:40 
GeneralGood Job. Many Thanks Pin
Nilkanth Naik19-Mar-15 22:22
MemberNilkanth Naik19-Mar-15 22:22 
QuestionVertical scrollbar? Pin
Member 110697335-Dec-14 3:08
MemberMember 110697335-Dec-14 3:08 
QuestionmCheckedListBox_ItemCheck not everywhere firing Pin
Member 1014656710-Feb-14 22:23
MemberMember 1014656710-Feb-14 22:23 
QuestionSaving the settings Pin
Member 1020482612-Aug-13 8:18
MemberMember 1020482612-Aug-13 8:18 
GeneralMy vote of 5 Pin
Ali_IND7-Jun-13 22:43
MemberAli_IND7-Jun-13 22:43 
QuestionThank you very much! Pin
miteleda9-Nov-12 16:23
Membermiteleda9-Nov-12 16:23 
QuestionSave column selection Pin
MJhaa5-Nov-12 9:57
MemberMJhaa5-Nov-12 9:57 
GeneralMy vote of 5 Pin
MJhaa31-Oct-12 8:38
MemberMJhaa31-Oct-12 8:38 
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
MemberYZK29-Mar-11 23:32 
GeneralThanks Pin
YZK29-Mar-11 23:30
MemberYZK29-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 

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.