Click here to Skip to main content
15,881,803 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 
Simple and dirty way (you can compare wit horiginal, to see what changed)
1. Changed MenuControl class:
<br />
    class MenuControl<br />
    {<br />
        private static int m_iImageColumnWidth = 24;<br />
        private static int m_iExtraWidth = 15;<br />
        private class MenuCommand<br />
        {<br />
            public enum eType<br />
            {<br />
                Column,<br />
                CheckAll,<br />
                CheckNone,<br />
                Done<br />
            }<br />
<br />
            public int Height { get { return Separator ? 5 : 21; } }<br />
            public bool Separator { get { return m_csText == "-"; } }<br />
<br />
            private string m_csText;<br />
            //private int m_iIndex;<br />
            private bool m_bChecked;<br />
            private eType m_eType;<br />
            public string Text { get { return m_csText; } }<br />
            //public int Index { get { return m_iIndex; } }<br />
            public eType MenuCommandType { get { return m_eType; } }<br />
            public bool Checked { get { return m_bChecked; } set { m_bChecked = value; } }<br />
            //public MenuCommand(string csText, int iIndex, bool bChecked)<br />
            public MenuCommand(string csText, bool bChecked)<br />
                : this(csText, bChecked, eType.Column)<br />
            {<br />
            }<br />
            public MenuCommand(string csText)<br />
                : this(csText, false, eType.Column)<br />
            {<br />
            }<br />
            public MenuCommand(string csText, bool bChecked, eType peType)<br />
            {<br />
                m_csText = csText;<br />
                //m_iIndex = iIndex;<br />
                m_bChecked = bChecked;<br />
                m_eType = peType;<br />
            }<br />
        }<br />
<br />
        private MenuCommand m_pTracMenuItem = null;<br />
        private List<MenuCommand> m_pMenuCommands = new List<MenuCommand>();<br />
<br />
        private Bitmap m_pMemBitmap;// = new Bitmap(panel1.Width, panel1.Height, PixelFormat.Format32bppArgb);<br />
        private Graphics m_pMemGraphics;<br />
<br />
        public int Width { get { return m_pMemBitmap.Width; } }<br />
        public int Height { get { return m_pMemBitmap.Height; } }<br />
<br />
        public bool Done<br />
        {<br />
            get<br />
            {<br />
                return m_pTracMenuItem != null && m_pTracMenuItem.MenuCommandType == MenuCommand.eType.Done;<br />
            }<br />
        }<br />
        public bool CheckAll<br />
        {<br />
            get<br />
            {<br />
                return m_pTracMenuItem != null && m_pTracMenuItem.MenuCommandType == MenuCommand.eType.CheckAll;<br />
            }<br />
        }<br />
        public bool CheckNone<br />
        {<br />
            get<br />
            {<br />
                return m_pTracMenuItem != null && m_pTracMenuItem.MenuCommandType == MenuCommand.eType.CheckNone;<br />
            }<br />
        }<br />
        public int HitIndex<br />
        {<br />
            get<br />
            {<br />
                return m_pMenuCommands.IndexOf(m_pTracMenuItem);<br />
            }<br />
        }<br />
<br />
        public bool ChangeChecked(int iIndex, Graphics g)<br />
        {<br />
            m_pMenuCommands[iIndex].Checked = !m_pMenuCommands[iIndex].Checked;<br />
            Draw(g);<br />
            return m_pMenuCommands[iIndex].Checked;<br />
        }<br />
<br />
        public void Add(string csText, bool bChecked)<br />
        {<br />
            m_pMenuCommands.Add(new MenuCommand(csText, bChecked));<br />
        }<br />
<br />
        public void CheckAllMenus(bool bCheck, Graphics g)<br />
        {<br />
            foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
            {<br />
                if (pMenuCommand.MenuCommandType == MenuCommand.eType.Column)<br />
                {<br />
                    pMenuCommand.Checked = bCheck;<br />
                }<br />
            }<br />
            Draw(g);<br />
        }<br />
<br />
        public void Prepare(Graphics g)<br />
        {<br />
            m_pMenuCommands.Add(new MenuCommand("-"));<br />
            m_pMenuCommands.Add(new MenuCommand("All", false, MenuCommand.eType.CheckAll));<br />
            m_pMenuCommands.Add(new MenuCommand("None", false, MenuCommand.eType.CheckNone));<br />
            m_pMenuCommands.Add(new MenuCommand("-"));<br />
            m_pMenuCommands.Add(new MenuCommand("Done", false, MenuCommand.eType.Done));<br />
<br />
            int iHeight = 4; //(2 + 2 top + bottom);<br />
            float fWidth = 0;<br />
            foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
            {<br />
                iHeight += pMenuCommand.Height;<br />
                SizeF pSizeF = g.MeasureString(pMenuCommand.Text, SystemInformation.MenuFont);<br />
                fWidth = Math.Max(fWidth, pSizeF.Width);<br />
            }<br />
            int iWidth = (int)fWidth + m_iImageColumnWidth + m_iExtraWidth;<br />
<br />
            m_pMemBitmap = new Bitmap(iWidth, iHeight);<br />
            m_pMemGraphics = Graphics.FromImage(m_pMemBitmap);<br />
        }<br />
<br />
        private MenuCommand HitTest(int X, int Y)<br />
        {<br />
            if (X < 0 || X > Width || Y < 0 || Y > Height)<br />
            {<br />
                return null;<br />
            }<br />
<br />
            int iHeight = 2;<br />
            foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
            {<br />
                if (Y > iHeight && Y < iHeight + pMenuCommand.Height)<br />
                {<br />
                    return pMenuCommand.Separator ? null : pMenuCommand;<br />
                }<br />
                iHeight += pMenuCommand.Height;<br />
            }<br />
            return null;<br />
        }<br />
<br />
        public bool HitTestMouseMove(int X, int Y)<br />
        {<br />
            MenuCommand pMenuCommand = HitTest(X, Y);<br />
            if (pMenuCommand != m_pTracMenuItem)<br />
            {<br />
                m_pTracMenuItem = pMenuCommand;<br />
                return true;<br />
            }<br />
            else<br />
            {<br />
                return false;<br />
            }<br />
        }<br />
        public bool HitTestMouseDown(int X, int Y)<br />
        {<br />
            MenuCommand pMenuCommand = HitTest(X, Y);<br />
            return pMenuCommand != null;<br />
        }<br />
<br />
        public void Draw(Graphics g)<br />
        {<br />
            Rectangle area = new Rectangle(0, 0, m_pMemBitmap.Width, m_pMemBitmap.Height);<br />
<br />
            m_pMemGraphics.Clear(SystemColors.Control);<br />
<br />
            // Draw the background area<br />
            DrawBackground(m_pMemGraphics, area);<br />
<br />
            // Draw the actual menu items<br />
            DrawAllCommands(m_pMemGraphics);<br />
<br />
            g.DrawImage(m_pMemBitmap, area, area, GraphicsUnit.Pixel);<br />
        }<br />
<br />
        private void DrawBackground(Graphics g, Rectangle rectWin)<br />
        {<br />
            Rectangle main = new Rectangle(0, 0, rectWin.Width, rectWin.Height);<br />
<br />
<br />
            int xStart = 1;<br />
            int yStart = 2;<br />
            int yHeight = main.Height - yStart - 1;<br />
<br />
            // Paint the main area background<br />
            using (Brush backBrush = new SolidBrush(Color.FromArgb(249, 248, 247)))<br />
                g.FillRectangle(backBrush, main);<br />
<br />
            // Draw single line border around the main area<br />
            using (Pen mainBorder = new Pen(Color.FromArgb(102, 102, 102)))<br />
                g.DrawRectangle(mainBorder, main);<br />
<br />
            Rectangle imageRect = new Rectangle(xStart, yStart, m_iImageColumnWidth, yHeight);<br />
<br />
            // Draw the first image column<br />
            using (Brush openBrush = new LinearGradientBrush(imageRect, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0f))<br />
                g.FillRectangle(openBrush, imageRect);<br />
<br />
            // Draw shadow around borders<br />
            int rightLeft = main.Right + 1;<br />
            int rightTop = main.Top + 4;<br />
            int rightBottom = main.Bottom + 1;<br />
            int leftLeft = main.Left + 4;<br />
            int xExcludeStart = main.Left;<br />
            int xExcludeEnd = main.Left;<br />
        }<br />
<br />
        private void DrawAllCommands(Graphics g)<br />
        {<br />
            int iTop = 2;<br />
            foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
            {<br />
                DrawSingleCommand(g, ref iTop, pMenuCommand, pMenuCommand == m_pTracMenuItem);<br />
            }<br />
        }<br />
<br />
        private void DrawSingleCommand(Graphics g, ref int iTop, MenuCommand pMenuCommand, bool hotCommand)<br />
        {<br />
            int iHeight = pMenuCommand.Height;<br />
            Rectangle drawRect = new Rectangle(1, iTop, Width, iHeight);<br />
            iTop += iHeight;<br />
<br />
            // Remember some often used values<br />
            int textGapLeft = 4;<br />
            int imageLeft = 4;<br />
<br />
            // Calculate some common values<br />
            int imageColWidth = 24;<br />
<br />
            // Is this item a separator?<br />
            if (pMenuCommand.Separator)<br />
            {<br />
                // Draw the image column background<br />
                Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top, imageColWidth, drawRect.Height);<br />
<br />
                // Draw the image column<br />
                using (Brush openBrush = new LinearGradientBrush(imageCol, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0f))<br />
                    g.FillRectangle(openBrush, imageCol);<br />
<br />
                // Draw a separator<br />
                using (Pen separatorPen = new Pen(Color.FromArgb(166, 166, 166)))<br />
                {<br />
                    // Draw the separator as a single line<br />
                    g.DrawLine(separatorPen,<br />
                               drawRect.Left + imageColWidth + textGapLeft, drawRect.Top + 2,<br />
                               drawRect.Right - 7,<br />
                               drawRect.Top + 2);<br />
                }<br />
            }<br />
            else<br />
            {<br />
                int leftPos = drawRect.Left + imageColWidth + textGapLeft;<br />
<br />
                // Should the command be drawn selected?<br />
                if (hotCommand)<br />
                {<br />
                    Rectangle selectArea = new Rectangle(drawRect.Left + 1, drawRect.Top, drawRect.Width - 9, drawRect.Height - 1);<br />
<br />
                    using (SolidBrush selectBrush = new SolidBrush(Color.FromArgb(182, 189, 210)))<br />
                        g.FillRectangle(selectBrush, selectArea);<br />
<br />
                    using (Pen selectPen = new Pen(Color.FromArgb(10, 36, 106)))<br />
                        g.DrawRectangle(selectPen, selectArea);<br />
                }<br />
                else<br />
                {<br />
                    Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top, imageColWidth, drawRect.Height);<br />
<br />
                    // Paint the main background color<br />
                    using (Brush backBrush = new SolidBrush(Color.FromArgb(249, 248, 247)))<br />
                        g.FillRectangle(backBrush, new Rectangle(drawRect.Left + 1, drawRect.Top, drawRect.Width - 9, drawRect.Height));<br />
<br />
                    using (Brush openBrush = new LinearGradientBrush(imageCol, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0f))<br />
                        g.FillRectangle(openBrush, imageCol);<br />
                }<br />
<br />
                // Calculate text drawing rectangle<br />
                Rectangle strRect = new Rectangle(<br />
                    leftPos,<br />
                    drawRect.Top,<br />
                    Width - imageColWidth - textGapLeft - 5,<br />
                    drawRect.Height);<br />
<br />
                // Left align the text drawing on a single line centered vertically<br />
                // and process the & character to be shown as an underscore on next character<br />
                StringFormat format = new StringFormat();<br />
                format.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap;<br />
                format.Alignment = StringAlignment.Near;<br />
                format.LineAlignment = StringAlignment.Center;<br />
                format.HotkeyPrefix = HotkeyPrefix.Show;<br />
<br />
                SolidBrush textBrush = new SolidBrush(SystemColors.MenuText);<br />
                g.DrawString(pMenuCommand.Text, SystemInformation.MenuFont, textBrush, strRect, format);<br />
<br />
                // The image offset from top of cell is half the space left after<br />
                // subtracting the height of the image from the cell height<br />
                int imageTop = drawRect.Top + (drawRect.Height - 16) / 2;<br />
<br />
                // Should a check mark be drawn?<br />
                if (pMenuCommand.Checked)<br />
                {<br />
                    Pen boxPen = new Pen(Color.FromArgb(10, 36, 106));<br />
                    Brush boxBrush;<br />
<br />
                    if (hotCommand)<br />
                        boxBrush = new SolidBrush(Color.FromArgb(133, 146, 181));<br />
                    else<br />
                        boxBrush = new SolidBrush(Color.FromArgb(212, 213, 216));<br />
<br />
                    Rectangle boxRect = new Rectangle(imageLeft - 1, imageTop - 1, 16 + 2, 16 + 2);<br />
<br />
                    // Fill the checkbox area very slightly<br />
                    g.FillRectangle(boxBrush, boxRect);<br />
<br />
                    // Draw the box around the checkmark area<br />
                    g.DrawRectangle(boxPen, boxRect);<br />
<br />
                    boxPen.Dispose();<br />
                    boxBrush.Dispose();<br />
<br />
                    Pen pPen = new Pen(Color.Black, 1);<br />
                    g.DrawLine(pPen, new Point(imageLeft + 5, imageTop + 8), new Point(imageLeft + 5 + 2, imageTop + 8 + 2));<br />
                    g.DrawLine(pPen, new Point(imageLeft + 5, imageTop + 9), new Point(imageLeft + 5 + 2, imageTop + 9 + 2));<br />
                    g.DrawLine(pPen, new Point(imageLeft + 5 + 2, imageTop + 8 + 2), new Point(imageLeft + 5 + 2 + 4, imageTop + 8 + 2 - 4));<br />
                    g.DrawLine(pPen, new Point(imageLeft + 5 + 2, imageTop + 9 + 2), new Point(imageLeft + 5 + 2 + 4, imageTop + 9 + 2 - 4));<br />
                }<br />
            }<br />
        }<br />
    }<br />


2. Changed UserControlMenu class:
<br />
    public partial class UserControlMenu : UserControl<br />
    {<br />
        public EventHandler DoneEvent;<br />
        public EventHandler CheckAllEvent;<br />
        public EventHandler CheckNoneEvent;<br />
        public delegate void CheckedChanged(int iIndex, bool bChecked);<br />
        public event CheckedChanged CheckedChangedEnent;<br />
        public virtual void OnCheckedChanged(int iIndex, bool bChecked)<br />
        {<br />
            if (CheckedChangedEnent != null)<br />
                CheckedChangedEnent(iIndex, bChecked);<br />
        }<br />
        public virtual void OnDone()<br />
        {<br />
            if (DoneEvent != null)<br />
                DoneEvent(this, EventArgs.Empty);<br />
        }<br />
        public virtual void OnCheckAll()<br />
        {<br />
            if (CheckAllEvent != null)<br />
                CheckAllEvent(this, EventArgs.Empty);<br />
        }<br />
        public virtual void OnCheckNone()<br />
        {<br />
            if (CheckNoneEvent != null)<br />
                CheckNoneEvent(this, EventArgs.Empty);<br />
        }<br />
<br />
        MenuControl m_pMenuControl = new MenuControl();<br />
<br />
        public UserControlMenu()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
<br />
        private void buttonDone_Click(object sender, EventArgs e)<br />
        {<br />
            Parent.Focus();<br />
        }<br />
<br />
        public void Initialize(DataGridView pDataGridView)<br />
        {<br />
            m_pMenuControl = new MenuControl();<br />
<br />
            foreach (DataGridViewColumn c in pDataGridView.Columns)<br />
            {<br />
                m_pMenuControl.Add(c.HeaderText, c.Visible);<br />
            }<br />
<br />
            m_pMenuControl.Prepare(CreateGraphics());<br />
<br />
            Width = m_pMenuControl.Width;<br />
            Height = m_pMenuControl.Height;<br />
<br />
            timer1.Enabled = true;<br />
<br />
        }<br />
<br />
        private void UserControlMenu_Paint(object sender, PaintEventArgs e)<br />
        {<br />
            m_pMenuControl.Draw(e.Graphics);<br />
        }<br />
<br />
        private void UserControlMenu_MouseMove(object sender, MouseEventArgs e)<br />
        {<br />
            if (m_pMenuControl.HitTestMouseMove(e.X, e.Y))<br />
            {<br />
                m_pMenuControl.Draw(CreateGraphics());<br />
            }<br />
        }<br />
<br />
        private void UserControlMenu_MouseDown(object sender, MouseEventArgs e)<br />
        {<br />
            if (m_pMenuControl.HitTestMouseDown(e.X, e.Y))<br />
            {<br />
                if (m_pMenuControl.Done)<br />
                {<br />
                    OnDone();<br />
                }<br />
                else if (m_pMenuControl.CheckAll)<br />
                {<br />
                    OnCheckAll();<br />
                    m_pMenuControl.CheckAllMenus(true, CreateGraphics());<br />
                }<br />
                else if (m_pMenuControl.CheckNone)<br />
                {<br />
                    OnCheckNone();<br />
                    m_pMenuControl.CheckAllMenus(false, CreateGraphics());<br />
                }<br />
                else<br />
                {<br />
                    int iHitIndex = m_pMenuControl.HitIndex;<br />
                    if (iHitIndex != -1)<br />
                    {<br />
                        bool bChecked = m_pMenuControl.ChangeChecked(iHitIndex, CreateGraphics());<br />
                        OnCheckedChanged(iHitIndex, bChecked);<br />
                    }<br />
                }<br />
            }<br />
        }<br />
<br />
        private void timer1_Tick(object sender, EventArgs e)<br />
        {<br />
            Point pPoint = PointToClient(Cursor.Position);<br />
            if (m_pMenuControl.HitTestMouseMove(pPoint.X, pPoint.Y))<br />
            {<br />
                m_pMenuControl.Draw(CreateGraphics());<br />
            }<br />
        }<br />
    }<br />


3. Changed DataGridViewColumnSelector class:
<br />
    class DataGridViewColumnSelector<br />
    {<br />
        // the DataGridView to which the DataGridViewColumnSelector is attached<br />
        private DataGridView mDataGridView = null;<br />
        // a CheckedListBox containing the column header text and checkboxes<br />
        private CheckedListBox mCheckedListBox;<br />
        // a ToolStripDropDown object used to show the popup<br />
        private ToolStripDropDown mPopup;<br />
        <br />
        /// <summary><br />
        /// The max height of the popup<br />
        /// </summary><br />
        public int MaxHeight = 300;<br />
        /// <summary><br />
        /// The width of the popup<br />
        /// </summary><br />
        public int Width = 200;<br />
<br />
        /// <summary><br />
        /// Gets or sets the DataGridView to which the DataGridViewColumnSelector is attached<br />
        /// </summary><br />
        public DataGridView DataGridView<br />
        {<br />
            get { return mDataGridView; }<br />
            set { <br />
                  // If any, remove handler from current DataGridView <br />
                  if (mDataGridView != null) mDataGridView.CellMouseClick -= new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);<br />
                  // Set the new DataGridView<br />
                  mDataGridView = value;<br />
                  // Attach CellMouseClick handler to DataGridView<br />
                  if (mDataGridView!=null) mDataGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);<br />
              }<br />
        }<br />
<br />
        // When user right-clicks the cell origin, it clears and fill the CheckedListBox with<br />
        // columns header text. Then it shows the popup. <br />
        // In this way the CheckedListBox items are always refreshed to reflect changes occurred in <br />
        // DataGridView columns (column additions or name changes and so on).<br />
        void mDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)<br />
        {<br />
            if (e.Button == MouseButtons.Right && e.RowIndex==-1 && e.ColumnIndex==-1) {<br />
                //mCheckedListBox.Items.Clear();<br />
                //foreach (DataGridViewColumn c in mDataGridView.Columns){<br />
                //    mCheckedListBox.Items.Add(c.HeaderText, c.Visible);<br />
                //}<br />
                //int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;<br />
                //mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;<br />
                //mCheckedListBox.Width = this.Width;<br />
                pUserControl1.Initialize(mDataGridView);<br />
                mPopup.Show(mDataGridView.PointToScreen(new Point (e.X,e.Y)));<br />
            }<br />
        }<br />
<br />
        UserControlMenu pUserControl1 = new UserControlMenu();<br />
        // The constructor creates an instance of CheckedListBox and ToolStripDropDown.<br />
        // the CheckedListBox is hosted by ToolStripControlHost, which in turn is<br />
        // added to ToolStripDropDown.<br />
        public DataGridViewColumnSelector() {<br />
            //mCheckedListBox = new CheckedListBox();<br />
            //mCheckedListBox.CheckOnClick = true;<br />
            //mCheckedListBox.ItemCheck += new ItemCheckEventHandler(mCheckedListBox_ItemCheck);<br />
<br />
            //ToolStripControlHost mControlHost = new ToolStripControlHost(mCheckedListBox);<br />
            pUserControl1.DoneEvent += new EventHandler(OnDone);<br />
            pUserControl1.CheckAllEvent += new EventHandler(OnCheckAll);<br />
            pUserControl1.CheckNoneEvent += new EventHandler(OnCheckNone);<br />
            pUserControl1.CheckedChangedEnent += new UserControlMenu.CheckedChanged(CheckedChangedEnent);<br />
            ToolStripControlHost mControlHost = new ToolStripControlHost(pUserControl1);<br />
            mControlHost.Padding = Padding.Empty;<br />
            mControlHost.Margin = Padding.Empty;<br />
            mControlHost.AutoSize = false;<br />
<br />
            mPopup = new ToolStripDropDown();<br />
            mPopup.Padding = Padding.Empty;<br />
            mPopup.AutoClose = true;<br />
            mPopup.Items.Add(mControlHost);<br />
        }<br />
<br />
        void CheckedChangedEnent(int iIndex, bool bChecked)<br />
        {<br />
            mDataGridView.Columns[iIndex].Visible = bChecked;<br />
        }<br />
<br />
        private void OnCheckAll(object sender, EventArgs e)<br />
        {<br />
            CheckAll(true);<br />
        }<br />
        private void OnCheckNone(object sender, EventArgs e)<br />
        {<br />
            CheckAll(false);<br />
        }<br />
        private void CheckAll(bool bChecked)<br />
        {<br />
            foreach (DataGridViewColumn pColumn in mDataGridView.Columns)<br />
            {<br />
                pColumn.Visible = bChecked;<br />
            }<br />
        }<br />
<br />
        private void OnDone(object sender, EventArgs e)<br />
        {<br />
            mPopup.AutoClose = false;<br />
            mPopup.Close();<br />
            mPopup.AutoClose = true;<br />
        }<br />
<br />
        public DataGridViewColumnSelector(DataGridView dgv) : this() {<br />
            this.DataGridView = dgv;<br />
        }<br />
<br />
        // When user checks / unchecks a checkbox, the related column visibility is <br />
        // switched.<br />
        void mCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e){<br />
            mDataGridView.Columns[e.Index].Visible = (e.NewValue == CheckState.Checked);<br />
        }<br />
    }<br />

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.