Click here to Skip to main content
14,547,071 members

A DataGridView Column Show/Hide Popup

Rate this:
4.80 (54 votes)
Please Sign up or sign in to vote.
4.80 (54 votes)
23 Dec 2008CPOL
A class adding column show/hide capability to a DataGridView
Image 1

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 <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;
	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.

I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Vivo così from the album L'immobile disegno (The immobile drawing).





"Short code, good code"



Comments and Discussions

 
GeneralThank you for the great article. [modified] Pin
Fiwel31-Aug-09 19:33
MemberFiwel31-Aug-09 19:33 
GeneralRe: Thank you for the great article. Pin
Fiwel31-Aug-09 19:34
MemberFiwel31-Aug-09 19:34 
GeneralRe: Thank you for the great article. Pin
Vincenzo Rossi1-Sep-09 8:46
MemberVincenzo Rossi1-Sep-09 8:46 
GeneralRe: Thank you for the great article. Pin
Vincenzo Rossi1-Sep-09 8:57
MemberVincenzo Rossi1-Sep-09 8:57 
GeneralRe: Thank you for the great article. Pin
Fiwel1-Sep-09 9:10
MemberFiwel1-Sep-09 9:10 
QuestionIs there a VC++ version? Pin
occamRT1-Jul-09 6:55
MemberoccamRT1-Jul-09 6:55 
AnswerRe: Is there a VC++ version? Pin
Vincenzo Rossi11-Jul-09 3:35
MemberVincenzo Rossi11-Jul-09 3:35 
GeneralContext Menu Strip Problem Pin
kapil bhavsar22-Jun-09 4:11
Memberkapil bhavsar22-Jun-09 4:11 
Thanx for the nice control.

My problem is i want to use ContextMenuStrip also with DGV but this doesnt let the ColumnSelector Popup to show. Any Solution to this will be very helpful.

Thanks Again.
GeneralThanks a lot Pin
amir897627-May-09 0:58
Memberamir897627-May-09 0:58 
NewsImplemented Indeterminate check status Pin
Deepak SHAW25-Feb-09 10:46
MemberDeepak SHAW25-Feb-09 10:46 
GeneralJust what I want Pin
RubyPdf10-Feb-09 3:20
MemberRubyPdf10-Feb-09 3:20 
GeneralRe: Just what I want Pin
Vincenzo Rossi10-Feb-09 9:55
MemberVincenzo Rossi10-Feb-09 9:55 
Generalgreat code Pin
Donsw5-Jan-09 14:06
MemberDonsw5-Jan-09 14:06 
GeneralCool Indeed Pin
AngleV29-Dec-08 8:30
MemberAngleV29-Dec-08 8:30 
GeneralRe: Cool Indeed Pin
Vincenzo Rossi29-Dec-08 12:59
MemberVincenzo Rossi29-Dec-08 12:59 
GeneralWay cool, Pin
BGaddis23-Dec-08 6:53
MemberBGaddis23-Dec-08 6:53 
GeneralRe: Way cool, Pin
Vincenzo Rossi23-Dec-08 7:14
MemberVincenzo Rossi23-Dec-08 7:14 

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.

Article
Posted 23 Dec 2008

Stats

168.4K views
8.2K downloads
147 bookmarked