Click here to Skip to main content
13,350,985 members (55,845 online)
Click here to Skip to main content
Add your own
alternative version


58 bookmarked
Posted 1 Sep 2009

A DataGridView Column Show/Hide Popup - Menu Style

, 1 Sep 2009
Rate this:
Please Sign up or sign in to vote.
A DataGridView column show/hide popup - Menu style.


I needed to allow my customers to be able to show/hide columns in a DataGridView.

Original source



There is a great article mentioned above (Thank you Vincenzo Rossi). It does exactly what I needed to do. (Please see the original article on how to use the ToolStripDropDown and ToolStripControlHost classes). The only thing I did not like - the usage of the check boxes - it seems to be old-fashioned... I replaced that with a menu-like control.

Using the code

Please refer to the original code if you have any questions regarding using the code. My additions are the UserControlMenu and MenuControl objects. Instead of using the original CheckedListBox, you will use UserControlMenu. You will need to define two events: OnDone and CheckedChangedEnent:

UserControlMenu pUserControl1 = new UserControlMenu();

public DataGridViewColumnSelector() {
    //mCheckedListBox = new CheckedListBox();
    //mCheckedListBox.CheckOnClick = true;
    //mCheckedListBox.ItemCheck += 
    //    new ItemCheckEventHandler(mCheckedListBox_ItemCheck);

    //ToolStripControlHost mControlHost = new ToolStripControlHost(mCheckedListBox);
    pUserControl1.DoneEvent += new EventHandler(OnDone);
    pUserControl1.CheckedChangedEnent += 
              new UserControlMenu.CheckedChanged(CheckedChangedEnent);
    ToolStripControlHost mControlHost = new ToolStripControlHost(pUserControl1);

void CheckedChangedEnent(int iIndex, bool bChecked)
    mDataGridView.Columns[iIndex].Visible = bChecked;

private void OnDone(object sender, EventArgs e)
    mPopup.AutoClose = false;
    mPopup.AutoClose = true;

On CellMouseClick, you will need to use a new object one more time:

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


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


About the Author

Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralLove This!!! Pin
aaroncampf10-Feb-11 6:59
memberaaroncampf10-Feb-11 6:59 
Generalchecking the checkbox Pin
Mphirana12-Aug-10 0:34
memberMphirana12-Aug-10 0:34 
GeneralRe: checking the checkbox Pin
Fiwel12-Aug-10 5:24
memberFiwel12-Aug-10 5:24 
GeneralRe: checking the checkbox Pin
ammarmujeeb10-Oct-10 11:01
memberammarmujeeb10-Oct-10 11:01 
Newsif you want using other ContextMenuStrip. I mean using two popup menu. Pin
DVasya8-Aug-10 22:11
memberDVasya8-Aug-10 22:11 
GeneralNew menu option Pin
pimpers9-Dec-09 17:01
memberpimpers9-Dec-09 17:01 
GeneralRe: New menu option Pin
Fiwel10-Dec-09 6:00
memberFiwel10-Dec-09 6:00 
GeneralRe: New menu option Pin
pimpers10-Dec-09 14:06
memberpimpers10-Dec-09 14:06 
GeneralRe: New menu option Pin
pimpers15-Dec-09 12:59
memberpimpers15-Dec-09 12:59 
QuestionVB version too ??? Pin
NQ19709-Sep-09 22:50
memberNQ19709-Sep-09 22:50 
AnswerRe: VB version too ??? Pin
Fiwel10-Sep-09 6:39
memberFiwel10-Sep-09 6:39 
Questionwhy only on cell/row index=-1? Pin
Unruled Boy1-Sep-09 16:21
memberUnruled Boy1-Sep-09 16:21 
AnswerRe: why only on cell/row index=-1? Pin
Fiwel2-Sep-09 5:41
memberFiwel2-Sep-09 5:41 
AnswerRe: why only on cell/row index=-1? Pin
Fiwel2-Sep-09 6:42
memberFiwel2-Sep-09 6:42 
GeneralIndicate Filter Pin
Ivo Closs1-Sep-09 16:19
memberIvo Closs1-Sep-09 16:19 
GeneralRe: Indicate Filter Pin
Fiwel2-Sep-09 6:33
memberFiwel2-Sep-09 6:33 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 1 Sep 2009
Article Copyright 2009 by Fiwel
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid