We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to set style on selected cells?

I'm using a sfDataGrid together with a custom StyleSelector because I need to apply some styles according to each cell contents. It works ok but when I select multiple cells, the SelectStyle method in the StyleSelector is not being called. So cells are being actually selected (I can copy them to clipboard and it works) but the selection is invisible, i.e. selected cells don't change its color. 
Even if SelectStyle was called, I don't know which would be the correct way to check if the cell being rendered is selected or not.
When I remove my custom StyleSelector, selected cells correctly get a grey background.
How could I achieve that? Thanks in advance.

6 Replies

AK Adhikesevan Kothandaraman Syncfusion Team May 14, 2019 02:15 PM UTC

Hi Carlos, 

Thanks for using Syncfusion products. 

We are currently working on your query with high priority. We will get back to you on this at 16th May, 2019.   
Thanks for your patience until then. 


DB Dinesh Babu Yadav Syncfusion Team May 17, 2019 06:23 AM UTC

Hi Carlos, 
Thanks for your patience.  
If you want to show the selection of row/cell when setting the background for SfDataGrid’s GridCell, you can change the opacity of the background color brush. Refer to the following KB document, 
Dinesh Babu Yadav 

CF Carlos Fernando Consigli May 25, 2019 02:29 AM UTC

Hi, sorry for the delay answering.
I've already read that documentation, but the problem is that I'm using a custom StyleSelector.
I'm doing something like this programatically:

newDatagrid.CellStyleSelector = new MyCellStyleSelector(newDatagrid);

and that MyCellStyleSelector has business logic that decides the color of the cell and returns the appropriate style.

I tried to do this:
   newDatagrid.RowSelectionBrush = new SolidColorBrush(Colors.Red);

but that only works if I don't assign my custom MyCellStyleSelector.

I'm overriding the SelectStyle method in my custom StyleSelector. I just need a way inside that method to know whether the cell being rendered is selected or not, so I can return the correct "selected" style.

JP Jagadeesan Pichaimuthu Syncfusion Team May 27, 2019 10:18 AM UTC

Hi Carlos, 
We analyzed your query to show row/cell selection when using CellStyleSelector. As mentioned in the KB, you need to change the opacity of the background color that you are applying in the cell style selector. On further analysis, we suspect that you are programmatically applying style and that you can programmatically change the opacity of background color as like below code snippet 
public class CustomStyleSelector : StyleSelector 
        public override Style SelectStyle(object item, DependencyObject container) 
           var businessObject = item as BusinessObjects; 
            Style cellStyle1 = new Style(typeof(GridCell)); 
cellStyle1.Setters.Add(new Setter(GridCell.BackgroundProperty, newSolidColorBrush(Colors.YellowGreen) { Opacity = 0.2 })); 
            Style cellStyle2 = new Style(typeof(GridCell)); 
            cellStyle2.Setters.Add(new Setter(GridCell.BackgroundProperty, newSolidColorBrush(Colors.CornflowerBlue) { Opacity = 0.2 })); 
            if (businessObject != null) 
                if (businessObject.EmployeeGender == "Male") 
                    return cellStyle1; 
                    return cellStyle2; 
            return base.SelectStyle(item, container); 
Further, you can check whether or not the item is selected using the code snippet below 
var businessObject = item as BusinessObjects; 
bool isSelected = dataGrid.SelectedItems.Contains(businessObject); 
Also please refer to the below KB articles to know more details on styling. 
KB Articles: 
If the above solution does not meet your requirement, please revert us with code files related to your CustomStyleSelector. If possible, please also revert us with modified sample. It will help us to investigate further and provide earlier prompt solution. 

CF Carlos Fernando Consigli May 28, 2019 06:15 AM UTC

Thanks for your answer. I will dig my code a little more and try to isolate my issue, maybe I'm messing it up somewhere because  pivotGrid.SelectedItems is always empty, even in the datagrid_SelectionChanged handler. However if I hit "ctrl+C", contents of all "invisibly" selected cells get copied to the clipboard and I can paste them in Excel. So they are actually selected somewhere. Still, both inside datagrid_SelectionChanged handler and in the SelectStyle method, I see always 0 elements in pivotGrid.SelectedItems.
I will try to make a simple sample where this is reproducible and upload it tomorrow.

DB Dinesh Babu Yadav Syncfusion Team May 29, 2019 04:27 AM UTC

Hi Carlos, 
Thanks for the update. We will wait to hear from you.  
Dinesh Babu Yadav 

Live Chat Icon For mobile
Up arrow icon