This is a migrated thread and some comments may be shown as answers.

Selectively Merging Cells in Specific Columns

5 Answers 389 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 11 Feb 2017, 03:25 AM

Hi,

I'm attempting to find a way to only merge adjacent cells in specific columns. For example if I have a RadGridView with a few columns and I want to Merge similar values that are adjacent to each other in Column[0] how would I do this?

I understand that the RadGridView.Merge* properties don't cover this specific use case but is there an event I could handle that would let me implement this type of functionality? It would need to be something where I can compare cells within a column and set whether they are merged or not.

Or maybe a method I could override?

I've attached a mockup in Excel of the type of merging that I'm looking to accomplish in RadGridView.

5 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 15 Feb 2017, 11:02 AM
Hi Anthony,

For your convenience, I prepared an example to demonstrate how to achieve the desired behavior. Please take a look at the implementation and consider how this approach fits your scenario.

I hope that this helps. Should you have any other questions, do not hesitate to contact us.
 
Regards,
Martin Vatev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Anthony
Top achievements
Rank 1
answered on 15 Feb 2017, 03:36 PM

Thanks Martin!

I see the IsCellMergingEnabled property is available in XAML but I'm unable to use this property as I don't know the names of my columns at compile time and I can't seem to access GridViewColumn.IsCellMergingEnabled in C# code.

Such as in the following:

01.foreach (GridViewColumn column in radGridView.Columns)
02.{
03.    if (column.UniqueName == "ColumnIWantToMerge")
04.    {
05.        column.IsCellMergingEnabled = true;
06.    }
07.    else
08.    {
09.        column.IsCellMergingEnabled = true;
10.    }
11.}

 

Is there a way to do this in code, perhaps an event before the RadGridView is drawn on screen?

0
Anthony
Top achievements
Rank 1
answered on 15 Feb 2017, 05:05 PM

My code example was incorrect:

 

foreach (GridViewColumn column in radGridView.Columns)
{
    if (column.UniqueName == "ColumnIWantToMerge")
    {
        column.IsCellMergingEnabled = true; // Will fail
    }
    else
    {
        column.IsCellMergingEnabled = false; // Will also fail
    }
}
0
Accepted
Martin
Telerik team
answered on 16 Feb 2017, 01:00 PM
Hi Anthony,

To achieve the desired behavior you can add an event handler to the AutoGeneratingColumn event and in it to perform the logic for disabling the cell merging. Please take a look at the code snippet below for reference:
public MainWindow()
   {
       InitializeComponent();
       this.radGridView.ItemsSource = EmployeeService.GetEmployees();
       this.radGridView.AutoGeneratingColumn += radGridView_AutoGeneratingColumn;
   }
    
 
   private void radGridView_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
   {
       GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
       if (dataColumn != null && dataColumn.UniqueName != "FirstName")
       {
           dataColumn.IsCellMergingEnabled = false;
       }
   }


Regards,
Martin Vatev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Anthony
Top achievements
Rank 1
answered on 16 Feb 2017, 03:21 PM
Excellent, thanks Martin!
Tags
GridView
Asked by
Anthony
Top achievements
Rank 1
Answers by
Martin
Telerik team
Anthony
Top achievements
Rank 1
Share this question
or