how to get the bound row data item in merged cell style selector

1 Answer 29 Views
GridView
Jeffrey
Top achievements
Rank 1
Iron
Jeffrey asked on 25 Oct 2023, 06:47 AM

I want to change the background of a merged cell based on the value of another column in the same row. In the SelectStyle method in class MergedCellStyleSelector which inherits from StyleSelector, I can only get the cell value but cannot get the bound entire data item, is there anyway I can get the bound row data item?

1 Answer, 1 is accepted

Sort by
0
Dinko
Telerik team
answered on 25 Oct 2023, 12:31 PM

Hello Jeffrey,

To style only some of the merged cells, you can use the MergedCellStyleSelector. In the SelectStyle method, you can return a custom style for the customization you need and null in order to use the default style. Can you try this and let me know if it helps?

Jeffrey
Top achievements
Rank 1
Iron
commented on 26 Oct 2023, 01:57 AM | edited

Hi Dinko:

Thanks for your answer, but I think this is not what I want, let me show you some demo code.

Let's say I have a Model named Employee

public class Employee

{

 

        private int age;

        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                OnPropertyChanged();
            }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged();
            }
        }

 

}

also I have an observable collection as the data source of radgridview. 

        private ObservableCollection<Employee> employees;

        public ObservableCollection<Employee> Employees
        {
            get { return employees; }
            set
            {
                employees= value;
                OnPropertyChanged();
            }
        }

Now I want to merge Age cells, I want to change the background of merged Age cells based on the value of "Name"(not "Age"), I want to obtain all the Employee data items corresponding to the current merged cell in the SelectStyle method, any idea?

 

thanks

-Jeffrey

Dinko
Telerik team
commented on 26 Oct 2023, 10:17 AM

Hello Jeffrey,

To achieve this requirement, you can use the MergedCellStyleSelector property of the RadGridView control. The MergedCellInfo class exposes a VerticalStart and Height properties which you can use to get the indexes of the underlying items and use the controls Employees collection to get the actual items and execute your logic.

public override Style SelectStyle(object item, DependencyObject container)
{
	var cellInfo = item as MergedCellInfo;
	var cell = container as GridViewMergedCell;
	var gridView = cell.ParentOfType<RadGridView>();
	var itemsCount = cellInfo.Height;
	var firstItemIndex = cellInfo.VerticalStart;
	var items = new List<Employee>();

        // Execute your logic with the names
}

For your convenience I prepared a sample solution where I implemented simple logic within the SelectStyle method to change the Age cell Background property. The result is as following:

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Jeffrey
Top achievements
Rank 1
Iron
Answers by
Dinko
Telerik team
Share this question
or