Category / Section
How to change the outermost border color of the WinForms DataGrid (SfDataGrid)?
1 min read
Change the outmost border color
SfDataGrid does not have built-in support to change the outermost border color. But this can be done by the following steps:
- Derive a control from System.Windows.Forms.Panel and add SfDataGrid to it.
- Set the Panel’s DockPadding.All to 1 and set the BackColor of Panel to the expected color.
- Set the BorderStyle to None for both Panel and SfDataGrid.
C#
public Form1() { InitializeComponent(); DataGridPanel dataGridPanel = new DataGridPanel(); this.Controls.Add(dataGridPanel); var data = new OrderInfoCollection(); dataGridPanel.DataGrid.DataSource = data.OrdersListDetails; } public class DataGridPanel : Panel { public SfDataGrid DataGrid = new SfDataGrid(); public DataGridPanel() : base() { this.DockPadding.All = 1; this.BackColor = Color.Blue; this.BorderStyle = BorderStyle.None; this.DataGrid.Dock = DockStyle.Fill; this.DataGrid.Style.BorderStyle = BorderStyle.None; MethodInfo method = this.DataGrid.GetType().GetMethod("UpdateStyles", BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(this.DataGrid, null); this.Controls.Add(this.DataGrid); } }
VB
Public Sub New() InitializeComponent() Dim dataGridPanel As New DataGridPanel() Me.Controls.Add(dataGridPanel) Dim data = New OrderInfoCollection() dataGridPanel.DataGrid.DataSource = data.OrdersListDetails End Sub Public Class DataGridPanel Inherits Panel Public DataGrid As New SfDataGrid() Public Sub New() MyBase.New() Me.DockPadding.All = 1 Me.BackColor = Color.Blue Me.BorderStyle = BorderStyle.None Me.DataGrid.Dock = DockStyle.Fill Me.DataGrid.Style.BorderStyle = BorderStyle.None Dim method As MethodInfo = Me.DataGrid.GetType().GetMethod("UpdateStyles", BindingFlags.NonPublic Or BindingFlags.Instance) method.Invoke(Me.DataGrid, Nothing) Me.Controls.Add(Me.DataGrid) End Sub End Class
Samples:
C# : BorderColor_CS
VB : BorderColor_VB