Click here to Skip to main content
Licence CPOL
First Posted 1 Sep 2009
Views 26,633
Downloads 1,296
Bookmarked 46 times

A DataGridView Column Show/Hide Popup - Menu Style

By | 1 Sep 2009 | Article
A DataGridView column show/hide popup - Menu style.

Introduction

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

Original source

DGVColumnSelector.aspx.

Background

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.Close();
    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) {
        //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;
        pUserControl1.Initialize(mDataGridView);
        mPopup.Show(mDataGridView.PointToScreen(new Point (e.X,e.Y)));
    }
}

License

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

About the Author

Fiwel

Software Developer (Senior)

United States United States

Member



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
GeneralLove This!!! Pinmemberaaroncampf5:59 10 Feb '11  
Generalchecking the checkbox PinmemberMphirana23:34 11 Aug '10  
GeneralRe: checking the checkbox PinmemberFiwel4:24 12 Aug '10  
GeneralRe: checking the checkbox Pinmemberammarmujeeb10:01 10 Oct '10  
Newsif you want using other ContextMenuStrip. I mean using two popup menu. PinmemberDVasya21:11 8 Aug '10  
GeneralNew menu option Pinmemberpimpers16:01 9 Dec '09  
GeneralRe: New menu option PinmemberFiwel5:00 10 Dec '09  
GeneralRe: New menu option Pinmemberpimpers13:06 10 Dec '09  
GeneralRe: New menu option Pinmemberpimpers11:59 15 Dec '09  
QuestionVB version too ??? PinmemberNQ197021:50 9 Sep '09  
AnswerRe: VB version too ??? PinmemberFiwel5:39 10 Sep '09  
Questionwhy only on cell/row index=-1? PinmemberUnruled Boy15:21 1 Sep '09  
AnswerRe: why only on cell/row index=-1? PinmemberFiwel4:41 2 Sep '09  
AnswerRe: why only on cell/row index=-1? PinmemberFiwel5:42 2 Sep '09  
GeneralIndicate Filter PinmemberIvo Closs15:19 1 Sep '09  
GeneralRe: Indicate Filter PinmemberFiwel5:33 2 Sep '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
Web02 | 2.5.120517.1 | Last Updated 1 Sep 2009
Article Copyright 2009 by Fiwel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid