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

A DataGridView Column Show/Hide Popup - Menu Style

, 1 Sep 2009 CPOL
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

Comments and Discussions

GeneralLove This!!! Pinmemberaaroncampf10-Feb-11 6:59 
Generalchecking the checkbox PinmemberMphirana12-Aug-10 0:34 
GeneralRe: checking the checkbox PinmemberFiwel12-Aug-10 5:24 
GeneralRe: checking the checkbox Pinmemberammarmujeeb10-Oct-10 11:01 
Newsif you want using other ContextMenuStrip. I mean using two popup menu. PinmemberDVasya8-Aug-10 22:11 
GeneralNew menu option Pinmemberpimpers9-Dec-09 17:01 
GeneralRe: New menu option PinmemberFiwel10-Dec-09 6:00 
GeneralRe: New menu option Pinmemberpimpers10-Dec-09 14:06 
GeneralRe: New menu option Pinmemberpimpers15-Dec-09 12:59 
QuestionVB version too ??? PinmemberNQ19709-Sep-09 22:50 
AnswerRe: VB version too ??? PinmemberFiwel10-Sep-09 6:39 
Questionwhy only on cell/row index=-1? PinmemberUnruled Boy1-Sep-09 16:21 
AnswerRe: why only on cell/row index=-1? PinmemberFiwel2-Sep-09 5:41 
AnswerRe: why only on cell/row index=-1? PinmemberFiwel2-Sep-09 6:42 
GeneralIndicate Filter PinmemberIvo Closs1-Sep-09 16:19 
GeneralRe: Indicate Filter PinmemberFiwel2-Sep-09 6:33 

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
Web04 | 2.8.150301.1 | Last Updated 1 Sep 2009
Article Copyright 2009 by Fiwel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid