BoldDeskBoldDesk is now live on Product Hunt with a special offer: 50% off all plans. Let's grow together! Support us.
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!
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.
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
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.