Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:31:45+00:00 2026-06-17T17:31:45+00:00

I am developing a WPF application.There is a datagrid in my Application i have

  • 0

I am developing a WPF application.There is a datagrid in my Application i have created a context menu to hide and unhide column header of the datagrid while assigning the itemsource of datagrid to an IEnumerable collection.

 this.dataGrid1.ItemsSource = objref.Result;


                grid_data = objref.Result;
                cxMenu = new ContextMenu();

                foreach (Microsoft.Windows.Controls.DataGridColumn item in dataGrid1.Columns)
                {
                    menuItem = new MenuItem();
                    menuItem.Header = item.Header;
                    menuItem.IsChecked = true;
                    cxMenu.Items.Add(menuItem);
                    menuItem.Click += new RoutedEventHandler(menuItem_Click);
                    menuItem.Checked += new RoutedEventHandler(menuItem_Checked);
                    menuItem.Unchecked += new RoutedEventHandler(menuItem_Unchecked);
                }

Everythjing working fine. when i uncheck the columns are successfully remobved but when i again check the MenuItem of my ContextMenu it is not added.

Handler of my check event is as follows.

 void menuItem_Checked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        dataGrid1.ItemsSource = null;
        dataGrid1.ItemsSource = objref.Result;// Again assgining the whole set to itemssource
        List<string> menuList = new List<string>();
        menuList.Clear();



    foreach (MenuItem menuItem in cxMenu.Items)
    {
        if (menuItem.IsChecked == false)
        {
            menuList.Add(menuItem.Header.ToString());
        }
    }

        Functionsclass objref = new Functionsclass();


        foreach (string menuItem in menuList)
        {
            foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
            {
                if (column.Header.ToString() == menuItem)
                {
                    dataGrid1.Columns.Remove(column);
                    break;
                }
            }
        }
    }

But my column is not added when i check again. Please help me on this.

Update 2:

 void menuItem_Click(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        if (item.IsChecked)
        {
            item.IsChecked = false;
        }
        else
        {
            item.IsChecked = true;
        }
    }

    void menuItem_Unchecked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
        {
            if (column.Header.ToString().Contains(item.Header.ToString()))
            {
                dataGrid1.Columns.Remove(column);
                break;
            }
        }
    }

Uncheck handler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:31:46+00:00Added an answer on June 17, 2026 at 5:31 pm

    If you just want to hide/show columns, I don’t think Removing/Adding columns is the right approach. I suggest you make use of the Visibility property of the column. set it to Visibility.Collapsed to hide it, then Visibility.Visible to make it visible again.

    column.Visibility = Visibility.Collapsed; // Column is hidden
    
    column.Visibility = Visibility.Visible; //Column is Visible
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a WPF application, and I have created a custom usercontrol because I
I am developing wpf application in C#. I have successfully created the setup project
Context: I'm developing a WPF application which will contain a lot of different screens.
I am developing a WPF application, and I need your advice. I have to
I am developing wpf application in C#. I have one button on which I
In a WPF application I'm developing, I intend to have a number of buttons
I have an application which I am developing using WPF\Prism\MVVM. All is going well
I am developing a WPF application, and have a TextBlock which I want to
I am currently developing a new WPF application and have the majority of my
I am developing UI for a Wpf application. I have designs build by designer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.