2

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.

3
  • when you are Unchecking the column name you are removing the column name from grid,you have code for that but when you check that back ,you need to add that column back to grid,where is that part of code. Commented Jan 23, 2013 at 10:28
  • For that only i have added the entire column again and removed the columns which is unchecked in context menu..LL update that code too Commented Jan 23, 2013 at 10:43
  • Both Checked and Unchecked you are removing the column??? Commented Jan 23, 2013 at 10:47

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

I have just changed my checked and Unchecked event handlers like below. Now its working fine..:)

//Unchecked handler

 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()))
            {
               column.Visibility = Visibility.Collapsed;
                break;
            }


        }
    }

// Checked handler

    void menuItem_Checked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;

        List<string> menuList = new List<string>();
        menuList.Clear();
         foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
        {
           if (column.Header.ToString().Contains(item.Header.ToString()))
            {
               column.Visibility = Visibility.Visible;
                break;
            }


        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.