Click here to Skip to main content
11,412,833 members (71,618 online)
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

 
QuestionVB.net version Pin
SumitSaha at 26-Apr-15 0:19
memberSumitSaha26-Apr-15 0:19 
QuestionDoing in VB.Net Pin
Member 11356992 at 25-Apr-15 12:40
memberMember 1135699225-Apr-15 12:40 
GeneralGood Job. Many Thanks Pin
Nilkanth Naik at 19-Mar-15 22:22
memberNilkanth Naik19-Mar-15 22:22 
QuestionVertical scrollbar? Pin
Member 11069733 at 5-Dec-14 3:08
memberMember 110697335-Dec-14 3:08 
QuestionmCheckedListBox_ItemCheck not everywhere firing Pin
Member 10146567 at 10-Feb-14 22:23
memberMember 1014656710-Feb-14 22:23 
QuestionSaving the settings Pin
Member 10204826 at 12-Aug-13 8:18
memberMember 1020482612-Aug-13 8:18 
GeneralMy vote of 5 Pin
Ali_IND at 7-Jun-13 22:43
memberAli_IND7-Jun-13 22:43 
QuestionThank you very much! Pin
miteleda at 9-Nov-12 16:23
membermiteleda9-Nov-12 16:23 
QuestionSave column selection Pin
MJhaa at 5-Nov-12 9:57
memberMJhaa5-Nov-12 9:57 
GeneralMy vote of 5 Pin
MJhaa at 31-Oct-12 8:38
memberMJhaa31-Oct-12 8:38 
GeneralMy vote of 5 Pin
Vincent Paukgyi at 24-Sep-12 23:22
memberVincent Paukgyi24-Sep-12 23:22 
QuestionGood Job Pin
Aliasgar M, Maimoon at 7-Sep-12 0:54
memberAliasgar M, Maimoon7-Sep-12 0:54 
GeneralMy vote of 5 Pin
ProEnggSoft at 29-Feb-12 20:21
memberProEnggSoft29-Feb-12 20:21 
Questionthanks your code! Pin
caohit at 18-Feb-12 4:15
membercaohit18-Feb-12 4:15 
QuestionWhat If I have Columns That Must Stay Hidden? Pin
pointeman1 at 30-Dec-11 4:01
memberpointeman130-Dec-11 4:01 
QuestionIt does not work when the datagridview has a contextmenu object Pin
wuhuacong at 20-Aug-11 18:14
memberwuhuacong20-Aug-11 18:14 
GeneralMy vote of 5 Pin
Member 7908741 at 6-Jul-11 10:14
memberMember 79087416-Jul-11 10:14 
GeneralThanks for your info.. Pin
YZK at 29-Mar-11 23:32
groupYZK29-Mar-11 23:32 
GeneralThanks Pin
YZK at 29-Mar-11 23:30
groupYZK29-Mar-11 23:30 
GeneralMy vote of 5 Pin
snake2k3y@rambler.ru at 18-Mar-11 21:57
membersnake2k3y@rambler.ru18-Mar-11 21:57 
GeneralMy vote of 5 Pin
btaz125 at 23-Feb-11 2:18
memberbtaz12523-Feb-11 2:18 
GeneralMy vote of 5 Pin
Wasiq Amjad Bhamla at 29-Jan-11 10:27
memberWasiq Amjad Bhamla29-Jan-11 10:27 
GeneralMy vote of 5 Pin
sajadjamalian at 27-Jan-11 21:57
membersajadjamalian27-Jan-11 21:57 
GeneralMy vote of 5 Pin
JunfengGuo at 5-Dec-10 15:24
memberJunfengGuo5-Dec-10 15:24 
GeneralMy vote of 5 Pin
Member 1590223 at 21-Oct-10 17:45
memberMember 159022321-Oct-10 17:45 
GeneralThanks a lot Pin
Sunvory at 29-Dec-09 16:09
memberSunvory29-Dec-09 16:09 
GeneralPerfect!! Pin
miyayo at 6-Nov-09 4:48
membermiyayo6-Nov-09 4:48 
GeneralGreat Article Pin
Manvendrak at 21-Oct-09 18:03
memberManvendrak21-Oct-09 18:03 
GeneralRe: Great Article Pin
Vincenzo Rossi at 22-Oct-09 9:48
memberVincenzo Rossi22-Oct-09 9:48 
GeneralThank you for the great article. [modified] Pin
Fiwel at 31-Aug-09 20:33
memberFiwel31-Aug-09 20:33 
GeneralRe: Thank you for the great article. Pin
Fiwel at 31-Aug-09 20:34
memberFiwel31-Aug-09 20:34 
GeneralRe: Thank you for the great article. Pin
Vincenzo Rossi at 1-Sep-09 9:46
memberVincenzo Rossi1-Sep-09 9:46 
GeneralRe: Thank you for the great article. Pin
Vincenzo Rossi at 1-Sep-09 9:57
memberVincenzo Rossi1-Sep-09 9:57 
GeneralRe: Thank you for the great article. Pin
Fiwel at 1-Sep-09 10:10
memberFiwel1-Sep-09 10:10 
QuestionIs there a VC++ version? Pin
occamRT at 1-Jul-09 7:55
memberoccamRT1-Jul-09 7:55 
AnswerRe: Is there a VC++ version? Pin
Vincenzo Rossi at 11-Jul-09 4:35
memberVincenzo Rossi11-Jul-09 4:35 
GeneralContext Menu Strip Problem Pin
kapil bhavsar at 22-Jun-09 5:11
memberkapil bhavsar22-Jun-09 5:11 
GeneralThanks a lot Pin
amir8976 at 27-May-09 1:58
memberamir897627-May-09 1:58 
NewsImplemented Indeterminate check status Pin
Member 389129 at 25-Feb-09 11:46
memberMember 38912925-Feb-09 11:46 
GeneralJust what I want Pin
Steed Soft at 10-Feb-09 4:20
memberSteed Soft10-Feb-09 4:20 
GeneralRe: Just what I want Pin
Vincenzo Rossi at 10-Feb-09 10:55
memberVincenzo Rossi10-Feb-09 10:55 
Generalgreat code Pin
Donsw at 5-Jan-09 15:06
memberDonsw5-Jan-09 15:06 
GeneralCool Indeed Pin
AngleV at 29-Dec-08 9:30
memberAngleV29-Dec-08 9:30 
GeneralRe: Cool Indeed Pin
Vincenzo Rossi at 29-Dec-08 13:59
memberVincenzo Rossi29-Dec-08 13:59 
GeneralWay cool, Pin
BGaddis at 23-Dec-08 7:53
memberBGaddis23-Dec-08 7:53 
GeneralRe: Way cool, Pin
Vincenzo Rossi at 23-Dec-08 8:14
memberVincenzo 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
Web03 | 2.8.150427.1 | Last Updated 23 Dec 2008
Article Copyright 2008 by Vincenzo Rossi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid