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
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.
Martin Vatev
Telerik by Progress
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. else08. {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?
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 }}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