Merge Rows and Merge Columns

Aug 13, 2008 at 7:38 PM
One feature that I need on the Grid does not appear to be there yet.  I wonder if it is planned?   It's called "Merge Rows"  and it allows one column to have a "virtual row height" based on the number of rows that have the same repeated data in the column.  So let's say in the DataGrid Sample (The very nice one that you can download to learn more about the DatGrid) that you sorted the First Name column on First name and then looked at the rows with the First Name of "James".  Instead of repeating "James" 5 times for the 5 rows that start with a first name of "James",  If we had a Merge rows feature, In the Name column the row would be 5 rows high and "James" would only appear once, in the middle of the  viewable portion of the row height.  Do you have any suggestions about how best to accomplish this?  I may have to write this feature for my Grid myself, but otherwise the Grid looks like a winner for my work.
Aug 15, 2008 at 2:19 AM


As you have already found, this is a feature we do not support. Here is a suggestion to achieve this effect with the current DataGrid implementation. The end goal is to have the look that excel has for this scenario. i.e "James" appears on the last row of the group of rows that have "James" as the First Name. I know this is slightly different from what you asked for but this is the simplest to explain and so I am going to go with this for now. I will explain the hardships in achieving what you asked for towards this end of this response.

1.       Do your own sorting by listening to DataGrid.Sorting event and marking the event handled when done.

2.       Create an intermediate collection that can store the items in your original DataSource in sorted order.

3.       Additionally manipulate the data in this intermediate collection such that only the last row in the sorted range of rows with identical FirstNames has a non-empty value for the FirstName property. All the rest of the rows in that range have an empty string in its place.

4.       Now connect the intermediate collection as the ItemsSource for the DataGrid. On displaying this information, you will see that the each duplicate first name appear only once on the last row in the range. The rest of the cells in that column are empty.

5.       Now you need to control the visibility of the GridLines so that they do not show the bottom edges for those cells in the FirstName column that are empty. To do this, you will first need to turn off the default Gridlines implementation by setting GridLinesVisibility=”None” on the DataGrid.

6.       You can then use Borders around your cells to draw the bottom and right edges to simulate GridLines. Note that you will need to use some Triggers/TypeConverters to ensure that the bottom edges aren’t drawn for those cells in the FirstName column that are empty.

About centering the contents of the cell in FirstName column spanning rows,

1.       What does this mean for row selection?

2.       How do you edit that cell?

3.       Lastly, this is very hard to achieve with our current layout model, which of using a single dimensional panel to layout the rows and a nested single dimensional panel per row that lays out cells. This row spanning behavior could be slightly better enabled with a 2D layout, but there are other challenges in that space that caused us to pick the former.


Hope this helps.


Varsha (MSFT)

Aug 15, 2008 at 11:58 AM

Hi Varshal

I kind of figured that it would not be supported and that I would have to do it my self.  While it would have been nice if Microsoft added the function, (I'm sure that one of the pro's would do a better job at this than I would) the really great thing is that you expose the source which will allow me to add this custom function.  Merge Rows is a commonly needed function, and it would be good for Microsoft to re-consider adding the implementation that you suggest, without making any basic changes to the datagrid design.

To answer your questions, my view is:

1.)  In other grids that I have used, I have even seen row selection unavailable if you clicked in the "virtual row".  The OrchidGrid offered by SpringSys does better however.  A better solution for the Microsoft datagrid would be to use multiselect for the range in the intermediate collection.

2.)  Cell editing is just to complex to consider in a merged row column.  If we allow an edit, the intermediate collection could be used to produce an edit for each of its common rows, but changing all first names from James to Robert, would just not make any sense.  Merge Rows is a function that is really more of a viewing funtion to improve the experience of the user,  when examining complex data from a database.  If I do this I am not going to allow editing of the merge rows in the merge column.


I think of two other issues, that have come up in the past:

A.) When using alternating row background colors, some grids determine the background of the merged row by the first row that forms the intermediate collection. (This leads to consective rows in the merged column with no alternation of color.)  It is far better to simply apply alternating colors to the virtural rows.  For a better UX the important thing is the user gets to easily visualize the "break" in common data in the merge columns rows.

B.)I have to think of one other thing that I don't think you covered.  Say the grid's viewable size exposes 10 Rows, but the data called for a "virtual row" height of 18 rows.  The old True Grid, that shipped with early Visual Basic, had a nice solution.  It centered the  data in the 18 rows, however if the viewable area was less, when you scrolled, the merge row data would move to the top or the bottom of the "view" but never go off the screen.

Well, I have a gift for you, As you might have noted, I work (and think) in Visual Basic (as a legion of other professional developers do as well).  Since the datagrid sample was in C#, the first thing I had to do was translate it to English so that I could understand the concepts.  Since I have done the conversion to VB, I would be happy to send it to you so you could make it available to others.  It has a couple of snags that I did not figure out yet in the syntax. (For example in the Function FindByPartName Result = produces an error).  However, most of the compex problems in syntax have all been taken care of in y version.  So, it would need a quick review by a VB expert to clean up the remaing details.

If you want it -give me instructions on how to upload it.


Aug 19, 2008 at 2:21 PM

Hi Bob,

Thanks for your insights on this feature. We will keep this in mind for future enhancements that we plan for the WPF DataGrid. As for your offer on sharing your solution, we are not accepting any external contributions through this channel at this time. Hence I would recommend that you blog about your solution and send across a link to it. Some of the regular bloggers in WPF could then add a pointer to your solution to bring it some additional attention.


Varsha (MSFT)

May 28, 2009 at 9:25 PM

Has anyone successfully accomplished this as  a demo or sample?  I am being asked is this is possible.  I'm making the initial assumption is that the "merged" data would be read only.


Let me know,