<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://xceed.com/CS/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tech Side : GroupDescriptions</title><link>http://xceed.com/CS/blogs/techside/archive/tags/GroupDescriptions/default.aspx</link><description>Tags: GroupDescriptions</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Debug Build: 61120.2)</generator><item><title>DataGrid Grouping Tricks</title><link>http://xceed.com/CS/blogs/techside/archive/2011/06/13/datagrid-grouping-tricks.aspx</link><pubDate>Mon, 13 Jun 2011 15:30:00 GMT</pubDate><guid isPermaLink="false">14592c03-f9d0-4f6b-b4cd-71e0e1b1f679:30551</guid><dc:creator>Michel [Xceed]</dc:creator><slash:comments>5</slash:comments><comments>http://xceed.com/CS/blogs/techside/comments/30551.aspx</comments><wfw:commentRss>http://xceed.com/CS/blogs/techside/commentrss.aspx?PostID=30551</wfw:commentRss><description>&lt;p&gt;This is my first blog at Xceed!&amp;nbsp; So I will keep it simple.... promise!&lt;br&gt;&amp;nbsp;&lt;br&gt;This will be the first out of a series of blogs that will be posted by our support staff under the name &lt;b&gt;The Tech Side&lt;/b&gt;. The purpose of these blogs is to demonstrate how to achieve tasks that are commonly asked by our clients.&lt;br&gt;&amp;nbsp;&lt;br&gt;In this post from &lt;b&gt;The Tech Side&lt;/b&gt;, I will show how to do a few tricks in &lt;a title="Xceed DataGrid for WPF" target="_blank" href="http://xceed.com/Grid_WPF_Intro.html"&gt;Xceed DataGrid for WPF&lt;/a&gt;'s grouping mechanism by answering the following recurring questions:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;How do I flatten and unflatten groups?&lt;/li&gt;&lt;li&gt;How can I automatically hide/unhide columns when they are grouped/ungrouped?&lt;/li&gt;&lt;li&gt;How can I prevent my end users from ungrouping a certain column while still allowing grouping and ungrouping of others?&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;br&gt;&lt;b&gt;1- How do I flatten and unflatten groups?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;This is the simplest of all, and is often missed by many.&lt;/p&gt;&lt;p&gt;You can simply set the &lt;a title="AreGroupsFlattened Property" target="_blank" href="http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid.v4.2~Xceed.Wpf.DataGrid.Views.TableflowView~AreGroupsFlattened.html"&gt;AreGroupsFlattened&lt;/a&gt; property to true or false on the &lt;a title="DataGridControl Class" target="_blank" href="http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid.v4.2~Xceed.Wpf.DataGrid.DataGridControl.html"&gt;DataGridControl&lt;/a&gt;'s view and, as the name implies, setting it to true would flatten the groups while setting it to false would well, unflatten them.&amp;nbsp; Here is the xaml code:&lt;/p&gt;&lt;p&gt;&lt;b&gt;&amp;lt;xcdg:DataGridControl&amp;nbsp;Name="grid1" &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ItemsSource="{Binding Source={StaticResource techSource1}}"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;lt;xcdg:DataGridControl.View&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;xcdg:TableflowView AreGroupsFlattened="True"/&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/xcdg:DataGridControl.View&amp;gt;&lt;br&gt;&amp;lt;/xcdg:DataGridControl&amp;gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;And to toggle the &lt;a title="AreGroupsFlattened Property" target="_blank" href="http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid.v4.2~Xceed.Wpf.DataGrid.Views.TableflowView~AreGroupsFlattened.html"&gt;AreGroupsFlattened &lt;/a&gt;from code behind, you can do the following:&lt;/p&gt;&lt;p&gt;&lt;b&gt;TableflowView view = grid1.View as TableflowView;&lt;br&gt;view.AreGroupsFlattened = !view.AreGroupsFlattened;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;2- How can I automatically hide/unhide columns when they are grouped/ungrouped?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;For this one, you need to handle the CollectionChanged event of the &lt;a title="DataGridGroupDescription Class" target="_blank" href="http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid.v4.2~Xceed.Wpf.DataGrid.DataGridGroupDescription.html"&gt;GroupDescriptions &lt;/a&gt;property and change the corresponding column's Visible property. Here's an example:&lt;/p&gt;&lt;p&gt;&lt;b&gt;grid2.ItemsSourceChangeCompleted += new EventHandler(grid2_ItemsSourceChangeCompleted);&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;void grid2_ItemsSourceChangeCompleted(object sender, EventArgs e)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp; DataGridCollectionView v ;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;v = (DataGridCollectionView)grid2.ItemsSource;&lt;br&gt;&amp;nbsp;&amp;nbsp; v.GroupDescriptions.CollectionChanged += new NotifyCollectionChangedEventHandler(c_CollectionChanged);&lt;br&gt;}&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;void c_CollectionChanged ( object&amp;nbsp; sender,&amp;nbsp; NotifyCollectionChangedEventArgs e)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (e.Action == NotifyCollectionChangedAction.Remove)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string fieldName =((DataGridGroupDescription)e.OldItems[0]). PropertyName;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;grid2.Columns[fieldName ].Visible = true;&lt;br&gt;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp; else&lt;br&gt;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string fieldName =((DataGridGroupDescription)e.NewItems[0]). PropertyName;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grid2.Columns[fieldName ].Visible =&amp;nbsp; false;&lt;br&gt;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;/b&gt;&lt;/p&gt;&lt;p&gt;The reason why we handle &lt;a title="ItemsSourceChangedCompleted Event" target="_blank" href="http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid.v4.2~Xceed.Wpf.DataGrid.DataGridControl~ItemsSourceChangeCompleted_EV.html"&gt;ItemsSourceChangeCompleted &lt;/a&gt;first is because we are sure that after that event, the GroupDescriptions are initialized and that's where we handle the GroupDescriptions.CollectionChanged event.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;b&gt;3- How can I prevent my end users from ungrouping a certain column while still allowing grouping and ungrouping of others?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;This one is a bit tricky, but after a few trials and errors, I was able to get it done, and in fact, the answer was pretty simple;&lt;/p&gt;&lt;p&gt;This can be achieved by handling the PreviewMouseMove event on &lt;a title="GroupByItem Class" target="_blank" href="http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid.v4.2~Xceed.Wpf.DataGrid.GroupByItem_members.html#"&gt;GroupByItem&lt;/a&gt;. In the handler, I simply check if the GroupByItem is being dragged, and if its field name corresponds to the column that I want to be un-groupable. If the 2 conditions where satisfied I simply set e.Handled = true on the event and that disables un-grouping of that column! Here's an example:&lt;/p&gt;&lt;p&gt;&lt;b&gt;&amp;lt;Style TargetType="{x:Type xcdg:GroupByItem}"&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;lt;Style.Setters&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;EventSetter Event="PreviewMouseMove" Handler="g_PreviewMouseMove"/&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/Style.Setters&amp;gt;&lt;br&gt;&amp;lt;/Style&amp;gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;void g_PreviewMouseMove(object sender, MouseEventArgs e)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;GroupLevelDescription gld = (GroupLevelDescription)(sender as GroupByItem).Content;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ((sender as GroupByItem).IsBeingDragged &amp;amp;&amp;amp; gld.FieldName == "Name")&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled =&amp;nbsp; true;&lt;br&gt;}&lt;/b&gt;&lt;/p&gt;&lt;p&gt;You can download the source code for the previous examples &lt;a target="_blank" href="http://download3.xceedsoft.com/packages//Temp/TechSideBlogs/michel/GroupingTipsAndTricks.zip"&gt;here&lt;/a&gt;, and don't forget to leave your comments and suggestions for future blog posts in the comments section below!&lt;/p&gt;&lt;p&gt;Michel &lt;/p&gt;&lt;img src="http://xceed.com/CS/aggbug.aspx?PostID=30551" width="1" height="1"&gt;</description><category domain="http://xceed.com/CS/blogs/techside/archive/tags/GroupByItem/default.aspx">GroupByItem</category><category domain="http://xceed.com/CS/blogs/techside/archive/tags/TechSide/default.aspx">TechSide</category><category domain="http://xceed.com/CS/blogs/techside/archive/tags/AreGroupsFlattened/default.aspx">AreGroupsFlattened</category><category domain="http://xceed.com/CS/blogs/techside/archive/tags/GroupDescriptions/default.aspx">GroupDescriptions</category></item></channel></rss>