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

Change background color of merged cells

6 Answers 29 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Amige
Top achievements
Rank 1
Veteran
Amige asked on 29 Jul 2020, 09:09 PM

I am using a GridView and I have a column with cell merging enabled (IsCellMergingEnabled="True"), also I have a style to set the aligment of the value of the cell:

<Style TargetType="telerik:GridViewMergedCell">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>

I would like to change the background color of the cell based on the value of that cell?

How can I do that?

I was thinking of using a converter but I don't know how can I pass the value of the cell as a parameter.

6 Answers, 1 is accepted

Sort by
0
Amige
Top achievements
Rank 1
Veteran
answered on 29 Jul 2020, 11:15 PM
I think I found the answer, using a Style Selector like in this link MergedCellsStyleSelector
0
Dilyan Traykov
Telerik team
answered on 03 Aug 2020, 05:04 AM

Hello Amige,

Indeed, using the MergedCellsStyleSelector property is the correct approach for setting conditional styling to the merged cells of the control.

I do hope you've managed to create the appropriate selector class, but if you require any further assistance in the process, please let me know.

Regards,
Dilyan Traykov
Progress Telerik

0
Amige
Top achievements
Rank 1
Veteran
answered on 05 Aug 2020, 03:42 PM

Hi Dilyan,

I was able to create the selector class, now I would like to use that class to style more than one merged column.

I was trying to get the information of each column to set the appropiate style, using:

var cell = container as GridViewMergedCell;
var column = cell.DataColumn;

 

but cell.DataColumn is always null, is there a way to obtain the DataMember or the Name of the columns?

 

Regards.

0
Accepted
Dilyan Traykov
Telerik team
answered on 07 Aug 2020, 11:17 AM

Hi Amige,

To obtain the column of the merged cell, you can use the HorizontalStart property of the MergedCellInfo class like so:

    public class CustomMergedCellStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            var cellInfo = item as MergedCellInfo;
            var cell = container as GridViewMergedCell;
            var columnIndex = cellInfo.HorizontalStart;
            var columns = cell.ParentDataControl.Columns;

            if (cellInfo != null && cellInfo.HorizontalStart == columns["Country"].DisplayIndex)
            {
                return this.CountryColumnStyle;
            }

            return base.SelectStyle(item, container);
        }

        public Style CountryColumnStyle { get; set; }
    }
Please note, however, that this will only work if you're using the Vertical MergedCellsDirection. If you're using the Horizontal setting, you may need to add additional logic to achieve the desired result.

For your convenience, I'm attaching a small sample project which demonstrates this approach. Please let me know if you find it helpful.

Regards,
Dilyan Traykov
Progress Telerik

0
Amige
Top achievements
Rank 1
Veteran
answered on 07 Aug 2020, 03:27 PM

Hi Dilyan,

That is exactly what I was looking for.

Thanks.

0
Dilyan Traykov
Telerik team
answered on 10 Aug 2020, 05:18 AM

Hi Amige,

I'm happy to hear you found my suggestion helpful. If I can be of any further assistance, please let me know.

Regards,
Dilyan Traykov
Progress Telerik

Tags
GridView
Asked by
Amige
Top achievements
Rank 1
Veteran
Answers by
Amige
Top achievements
Rank 1
Veteran
Dilyan Traykov
Telerik team
Share this question
or