How to Hide and Un-Hide columns with context Menu In DataGrid In WPF

No.of Views1004
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  02 May 2010 00:05:56
Tag : WPF , Grid Controls
In this article we will see how we can add a ContextMenu for the Header and that will perform our Hide/Un-Hide function.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In my article on “Hide/Un-Hide Columns In DataGrid In WPF”, we saw how to use it using CheckBoxes. In this article we will see how we can add a ContextMenu for the Header and that will perform our Hide/Un-Hide function.

Creating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as ContextMenuDGWPF.
 

Image Loading

First we need to create some sample data that will be loaded into DataGrid.
 

Image Loading

Above class is for our Employee Data to be bound to DataGrid.Add the following code into Constructor of the Window.


 

Image Loading

Now run the application to see if Data is displayed!

Image Loading

Great!Now we will tweak a litle bit. We will add the event handler AutoGeneratedColumns for DataGrid.Add the handler in the constructor.

 

Image Loading

Now in the above added event we will generate our ContextMenu.
 

Image Loading

As you see in the above code we are looping through all the columns and adding a MenuItem to ContextMenu each time.We need to add the above event handlers for MenuItem as we are going to Click/Check/Uncheck them at runtime.

Now in the Click event we will add the following code:
 

Image Loading

Here is the logic; when we click on the MenuItem only the Click event is handled, not the Check/Uncheck event. So we have Changed the IsChecked property each time the MenuItem is clicked. Then we can handle the Checked/Unchecked events.Now we will do similar thing that we did in our last article.

Add the following code for Checked event of MenuItem.
 

Image Loading

Add the following code for UnChecked event of MenuItem.

Image Loading

Next is our major part where we will handle the RightMouseButtonDown event and identify whether we have right clicked on the Header or not.

 

Image Loading

It seems we are ready. Run the application.

 

Image Loading

As you see above we have successfully displayed the ContextMenu on Column Header.Now we will perform the Hide/Un-Hide functions.


 

Image Loading

Conclusion

In this article, i have explianed, hide/un-hide the column  in WPF, Hope this article helps

Sample Project Source 

Download source files -21 kb

 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top