Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#

A DataGridView Column Show/Hide Popup - Menu Style

Rate me:
Please Sign up or sign in to vote.
4.65/5 (10 votes)
1 Sep 2009CPOL 71.5K   3.9K   59   16
A DataGridView column show/hide popup - Menu style.

Image 1

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:

C#
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:

C#
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralLove This!!! Pin
aaroncampf10-Feb-11 5:59
aaroncampf10-Feb-11 5:59 
Generalchecking the checkbox Pin
Mphirana11-Aug-10 23:34
Mphirana11-Aug-10 23:34 
GeneralRe: checking the checkbox Pin
Fiwel12-Aug-10 4:24
Fiwel12-Aug-10 4:24 
GeneralRe: checking the checkbox Pin
ammarmujeeb10-Oct-10 10:01
ammarmujeeb10-Oct-10 10:01 
Newsif you want using other ContextMenuStrip. I mean using two popup menu. Pin
DVasya8-Aug-10 21:11
DVasya8-Aug-10 21:11 
GeneralNew menu option Pin
pimpers9-Dec-09 16:01
pimpers9-Dec-09 16:01 
GeneralRe: New menu option Pin
Fiwel10-Dec-09 5:00
Fiwel10-Dec-09 5:00 
GeneralRe: New menu option Pin
pimpers10-Dec-09 13:06
pimpers10-Dec-09 13:06 
GeneralRe: New menu option Pin
pimpers15-Dec-09 11:59
pimpers15-Dec-09 11:59 
QuestionVB version too ??? Pin
NQ19709-Sep-09 21:50
NQ19709-Sep-09 21:50 
AnswerRe: VB version too ??? Pin
Fiwel10-Sep-09 5:39
Fiwel10-Sep-09 5:39 
Questionwhy only on cell/row index=-1? Pin
Huisheng Chen1-Sep-09 15:21
Huisheng Chen1-Sep-09 15:21 
AnswerRe: why only on cell/row index=-1? Pin
Fiwel2-Sep-09 4:41
Fiwel2-Sep-09 4:41 
AnswerRe: why only on cell/row index=-1? Pin
Fiwel2-Sep-09 5:42
Fiwel2-Sep-09 5:42 
GeneralIndicate Filter Pin
Ivo Closs1-Sep-09 15:19
Ivo Closs1-Sep-09 15:19 
GeneralRe: Indicate Filter Pin
Fiwel2-Sep-09 5:33
Fiwel2-Sep-09 5: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.