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

A DataGridView Column Show/Hide Popup - Menu Style

By , 1 Sep 2009
Rate this:
Please Sign up or sign in to vote.

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
No Biography provided

Comments and Discussions

 
GeneralLove This!!! Pinmemberaaroncampf10-Feb-11 5:59 
Generalchecking the checkbox PinmemberMphirana11-Aug-10 23:34 
GeneralRe: checking the checkbox PinmemberFiwel12-Aug-10 4:24 
GeneralRe: checking the checkbox Pinmemberammarmujeeb10-Oct-10 10:01 
Newsif you want using other ContextMenuStrip. I mean using two popup menu. PinmemberDVasya8-Aug-10 21:11 
GeneralNew menu option Pinmemberpimpers9-Dec-09 16:01 
GeneralRe: New menu option PinmemberFiwel10-Dec-09 5:00 
GeneralRe: New menu option Pinmemberpimpers10-Dec-09 13:06 
GeneralRe: New menu option Pinmemberpimpers15-Dec-09 11:59 
QuestionVB version too ??? PinmemberNQ19709-Sep-09 21:50 
AnswerRe: VB version too ??? PinmemberFiwel10-Sep-09 5:39 
Questionwhy only on cell/row index=-1? PinmemberUnruled Boy1-Sep-09 15:21 
AnswerRe: why only on cell/row index=-1? PinmemberFiwel2-Sep-09 4:41 
AnswerRe: why only on cell/row index=-1? PinmemberFiwel2-Sep-09 5:42 
GeneralIndicate Filter PinmemberIvo Closs1-Sep-09 15:19 
GeneralRe: Indicate Filter PinmemberFiwel2-Sep-09 5: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 | Mobile
Web04 | 2.8.140421.2 | Last Updated 1 Sep 2009
Article Copyright 2009 by Fiwel
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid