Articles in this section
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:

  1. Derive a control from System.Windows.Forms.Panel and add SfDataGrid to it.
  2. Set the Panel’s DockPadding.All to 1 and set the BackColor of Panel to the expected color.
  3. 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

 

Changed the outmost border color of DataGrid

Samples:

C# : BorderColor_CS

VB : BorderColor_VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied