Click here to Skip to main content
Licence CPOL
First Posted 23 Dec 2008
Views 58,772
Downloads 1,651
Bookmarked 95 times

A DataGridView Column Show/Hide Popup

By Vincenzo Rossi | 23 Dec 2008
A class adding column show/hide capability to a DataGridView

1

2
1 vote, 3.4%
3
5 votes, 17.2%
4
23 votes, 79.3%
5
4.75/5 - 29 votes
1 removed
μ 4.67, σa 0.91 [?]

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)

About the Author

Vincenzo Rossi

Software Developer (Senior)

Italy Italy

Member
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"




Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat If I have Columns That Must Stay Hidden? Pinmemberpointeman14:01 30 Dec '11  
QuestionIt does not work when the datagridview has a contextmenu object Pinmemberwuhuacong18:14 20 Aug '11  
GeneralMy vote of 5 PinmemberMember 790874110:14 6 Jul '11  
GeneralThanks for your info.. PingroupYZK23:32 29 Mar '11  
GeneralThanks PingroupYZK23:30 29 Mar '11  
GeneralMy vote of 5 Pinmembersnake2k3y@rambler.ru21:57 18 Mar '11  
GeneralMy vote of 5 Pinmemberbtaz1252:18 23 Feb '11  
GeneralMy vote of 5 PinmemberWasiq Amjad Bhamla10:27 29 Jan '11  
GeneralMy vote of 5 Pinmembersajadjamalian21:57 27 Jan '11  
GeneralMy vote of 5 PinmemberJunfengGuo15:24 5 Dec '10  
GeneralMy vote of 5 PinmemberMember 159022317:45 21 Oct '10  
GeneralThanks a lot PinmemberSunvory16:09 29 Dec '09  
GeneralPerfect!! Pinmembermiyayo4:48 6 Nov '09  
GeneralGreat Article PinmemberManvendrak18:03 21 Oct '09  
GeneralRe: Great Article PinmemberVincenzo Rossi9:48 22 Oct '09  
GeneralThank you for the great article. [modified] PinmemberFiwel20:33 31 Aug '09  
GeneralRe: Thank you for the great article. PinmemberFiwel20:34 31 Aug '09  
GeneralRe: Thank you for the great article. PinmemberVincenzo Rossi9:46 1 Sep '09  
GeneralRe: Thank you for the great article. PinmemberVincenzo Rossi9:57 1 Sep '09  
GeneralRe: Thank you for the great article. PinmemberFiwel10:10 1 Sep '09  
QuestionIs there a VC++ version? PinmemberoccamRT7:55 1 Jul '09  
AnswerRe: Is there a VC++ version? PinmemberVincenzo Rossi4:35 11 Jul '09  
GeneralContext Menu Strip Problem Pinmemberkapil bhavsar5:11 22 Jun '09  
GeneralThanks a lot Pinmemberamir89761:58 27 May '09  
NewsImplemented Indeterminate check status PinmemberMember 38912911:46 25 Feb '09  

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.

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