Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

Background

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;
	mPopup.Items.Add(mControlHost);
}

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) {
		mCheckedListBox.Items.Clear();
		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.

History

  • 23rd December, 2008: Initial post

License

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

Share

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"



Comments and Discussions

 
QuestionVertical scrollbar? PinmemberMember 110697335-Dec-14 3:08 
QuestionmCheckedListBox_ItemCheck not everywhere firing PinmemberMember 1014656710-Feb-14 22:23 
QuestionSaving the settings PinmemberMember 1020482612-Aug-13 8:18 
GeneralMy vote of 5 PinmemberAli_IND7-Jun-13 22:43 
QuestionThank you very much! Pinmembermiteleda9-Nov-12 16:23 
QuestionSave column selection PinmemberMJhaa5-Nov-12 9:57 
GeneralMy vote of 5 PinmemberMJhaa31-Oct-12 8:38 
GeneralMy vote of 5 PinmemberVincent Paukgyi24-Sep-12 23:22 
Very useful and neat
QuestionGood Job PinmemberAliasgar M, Maimoon7-Sep-12 0:54 
GeneralMy vote of 5 PinmemberProEnggSoft29-Feb-12 20:21 
Questionthanks your code! Pinmembercaohit18-Feb-12 4:15 
QuestionWhat If I have Columns That Must Stay Hidden? Pinmemberpointeman130-Dec-11 4:01 
QuestionIt does not work when the datagridview has a contextmenu object Pinmemberwuhuacong20-Aug-11 18:14 
GeneralMy vote of 5 PinmemberMember 79087416-Jul-11 10:14 
GeneralThanks for your info.. PingroupYZK29-Mar-11 23:32 
GeneralThanks PingroupYZK29-Mar-11 23:30 
GeneralMy vote of 5 Pinmembersnake2k3y@rambler.ru18-Mar-11 21:57 
GeneralMy vote of 5 Pinmemberbtaz12523-Feb-11 2:18 
GeneralMy vote of 5 PinmemberWasiq Amjad Bhamla29-Jan-11 10:27 
GeneralMy vote of 5 Pinmembersajadjamalian27-Jan-11 21:57 
GeneralMy vote of 5 PinmemberJunfengGuo5-Dec-10 15:24 
GeneralMy vote of 5 PinmemberMember 159022321-Oct-10 17:45 
GeneralThanks a lot PinmemberSunvory29-Dec-09 16:09 
GeneralPerfect!! Pinmembermiyayo6-Nov-09 4:48 
GeneralGreat Article PinmemberManvendrak21-Oct-09 18:03 
GeneralRe: Great Article PinmemberVincenzo Rossi22-Oct-09 9:48 
GeneralThank you for the great article. [modified] PinmemberFiwel31-Aug-09 20:33 
GeneralRe: Thank you for the great article. PinmemberFiwel31-Aug-09 20:34 
GeneralRe: Thank you for the great article. PinmemberVincenzo Rossi1-Sep-09 9:46 
GeneralRe: Thank you for the great article. PinmemberVincenzo Rossi1-Sep-09 9:57 
GeneralRe: Thank you for the great article. PinmemberFiwel1-Sep-09 10:10 
QuestionIs there a VC++ version? PinmemberoccamRT1-Jul-09 7:55 
AnswerRe: Is there a VC++ version? PinmemberVincenzo Rossi11-Jul-09 4:35 
GeneralContext Menu Strip Problem Pinmemberkapil bhavsar22-Jun-09 5:11 
GeneralThanks a lot Pinmemberamir897627-May-09 1:58 
NewsImplemented Indeterminate check status PinmemberMember 38912925-Feb-09 11:46 
GeneralJust what I want PinmemberSteed Soft10-Feb-09 4:20 
GeneralRe: Just what I want PinmemberVincenzo Rossi10-Feb-09 10:55 
Generalgreat code PinmemberDonsw5-Jan-09 15:06 
GeneralCool Indeed PinmemberAngleV29-Dec-08 9:30 
GeneralRe: Cool Indeed PinmemberVincenzo Rossi29-Dec-08 13:59 
GeneralWay cool, PinmemberBGaddis23-Dec-08 7:53 
GeneralRe: Way cool, PinmemberVincenzo Rossi23-Dec-08 8:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web02 | 2.8.141223.1 | Last Updated 23 Dec 2008
Article Copyright 2008 by Vincenzo Rossi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid