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 change backcolor and forecolor of specific cell when selected


I'm using SfDatagrid v20.4.0.51 in Winforms vb.NET

As you can see, the above screenshot. I manage to change the color of specific columns on the form load

however, when I select a row, that specific column color does not retain its color. 

I successfully did this in the Default Windows Form control  Datagridview. But  I'm new to using this control, It will be a big help if you have some ideas Thank you!


3 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team March 1, 2023 02:30 PM UTC

Hi James Amestoso Palin,


We have checked the feasibility to achieve your requirement. In that, whenever you are performing the selection with the Styles you used the selection-related appearance should be changed. Because the selection has a higher priority than the Styles. It is the default behavior of our SfDataGrid. If you need to overcome this, then you need to override the renderers of the respective columns.


Regards,
Dhanasekar M.




JA James Amestoso Palin replied to Dhanasekar Mohanraj March 2, 2023 01:08 PM UTC

HI  Dhanasekar Mohanraj, Thank you for assisting. I already got it just like you said by overriding the drawcell


for those who want the code and sample.


AddHandler sfDataGrid.DrawCell, AddressOf OnDrawCell

 

Private Sub OnDrawCell(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs)

              'here customize based on your scenario

              If e.Column.MappingName = "Country" Then

                             'set customized Style for sepecfix column

                             e.Style.BackColor = Color.Green

                             e.Style.TextColor = Color.White

 

                             Dim format As New StringFormat()

                             Dim drawingHelper As New DrawingHelper()

                             If e.Column.CellType = "Numeric" Then

                                           e.Style.HorizontalAlignment = HorizontalAlignment.Right

                             End If

                             Dim gethorizontalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Static, Type.DefaultBinder, New Type() {GetType(HorizontalAlignment)}, New ParameterModifier() {}).Invoke(drawingHelper, New Object() {e.Style.HorizontalAlignment})

                             Dim verticalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Static, Type.DefaultBinder, New Type() { GetType(VerticalAlignment) }, New ParameterModifier() { }).Invoke(drawingHelper, New Object() { e.Style.VerticalAlignment })

                             format.Alignment = CType(gethorizontalAlignment, StringAlignment)

                             format.LineAlignment = CType(verticalAlignment, StringAlignment)

                             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

 

              ElseIf e.Column.MappingName = "ShipCity" Then

                             'set customized Style for sepecfix column

                             e.Style.BackColor = Color.Red

                             e.Style.TextColor = Color.White

 

                             Dim format As New StringFormat()

                             Dim drawingHelper As New DrawingHelper()

                             If e.Column.CellType = "Numeric" Then

                                           e.Style.HorizontalAlignment = HorizontalAlignment.Right

                             End If

                             Dim gethorizontalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Static, Type.DefaultBinder, New Type() {GetType(HorizontalAlignment)}, New ParameterModifier() {}).Invoke(drawingHelper, New Object() {e.Style.HorizontalAlignment})

                             Dim verticalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Static, Type.DefaultBinder, New Type() { GetType(VerticalAlignment) }, New ParameterModifier() { }).Invoke(drawingHelper, New Object() { e.Style.VerticalAlignment })

                             format.Alignment = CType(gethorizontalAlignment, StringAlignment)

                             format.LineAlignment = CType(verticalAlignment, StringAlignment)

                             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

              End If

 

 

End Sub


Attachment: Sample_13e44999.zip


DM Dhanasekar Mohanraj Syncfusion Team March 3, 2023 01:55 PM UTC

James Amestoso Palin,


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.


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon