Overriding "SelectionStyle" Color with Color applied in "DrawCell" Event


Hi,


I have a DrawCell event that creates some color schemes for some cells while loading the grid.

Now what happens is that while selecting, it got covered by the color scheme defined in "Selectionstyle"

The requirement is that while selecting, Color should remain for those cells and the selectionstyle need to apply for remaining cells.

Is it possible ?

Selection Settings :

SelectionMode --> Single

SelectionUnit --> Row


Thanks,

An


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team May 31, 2021 06:31 AM UTC

Hi Anooj baby,

Thank you for contacting Syncfusion Support.

Your requirement can be achieved by handle the CellStyle applied case in DrawCell event in SfDataGrid. Please refer the below code snippet, 
sfDataGrid.DrawCell += SfDataGrid_DrawCell; 
 
private void SfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e){ 
            if (e.Column.MappingName == "CustomerName" || e.Column.MappingName == "Country") 
            { 
                if (e.DisplayText == "Thomas Hardy" || e.DisplayText == "Mexico") 
                { 
                    e.Style.BackColor = Color.Coral; 
                    e.Style.TextColor = Color.White; 
 
                    StringFormat format = new StringFormat(); 
                    DrawingHelper drawingHelper = new DrawingHelper(); 
                    if (e.Column.CellType == "Numeric") 
                        e.Style.HorizontalAlignment = HorizontalAlignment.Right; 
                    var horizontalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static, Type.DefaultBinder, new Type[] { typeof(HorizontalAlignment) }, new ParameterModifier[] { }).Invoke(drawingHelper, new object[] { e.Style.HorizontalAlignment }); 
                    var verticalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static, Type.DefaultBinder, new Type[] { typeof(VerticalAlignment) }, new ParameterModifier[] { }).Invoke(drawingHelper, new object[] { e.Style.VerticalAlignment }); 
                    format.Alignment = (StringAlignment)horizontalAlignment; 
                    format.LineAlignment = (StringAlignment)verticalAlignment; 
                    e.Graphics.FillRectangle(new SolidBrush(e.Style.BackColor), e.Bounds); 
                    e.Graphics.DrawString(e.DisplayText, e.Style.GetFont(), new SolidBrush(e.Style.TextColor), e.Bounds, format); 
                    e.Graphics.DrawLine(new Pen(e.Style.Borders.Right.Color), e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom); 
                    e.Graphics.DrawLine(new Pen(e.Style.Borders.Bottom.Color), e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1); 
                    //Handling the event  
                    e.Handled = true; 
                } 
            } 
} 
Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Marked as answer

AB anooj baby June 8, 2021 09:27 AM UTC

Hi Vijayarasan,

Thanks a lot for the help.
It really worked.

Cheers !!


VS Vijayarasan Sivanandham Syncfusion Team June 9, 2021 05:07 AM UTC

Hi Anooj baby,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards, 
Vijayarasan S 


Loader.
Up arrow icon