How to change background color of Header alone in Grid?
The Background color for Header row can be changed by setting the SfDataGrid.HeaderStyle property for DetailsView WPF DataGrid When defining SfDataGrid.DetailsViewDefinition, you can customize the property setting of DetailsView Grid by defining GridViewDefinition.DataGrid.HeaderStyle.
In the following code example, GridHeaderCellControl style is customized for DetailsViewGrid via GridViewDefinition.DataGrid.
XAML
<Window.DataContext> <local:ViewModel /> </Window.DataContext> <Grid> <syncfusion:SfDataGrid x:Name="dataGrid" AutoGenerateColumns="False" HideEmptyGridViewDefinition="True" ItemsSource="{Binding Path=OrdersDetails}" syncfusion:DetailsViewManager.DisableLastColumnResizing="True" > <syncfusion:SfDataGrid.DetailsViewDefinition> <syncfusion:GridViewDefinition RelationalColumn="OrderDetails"> <syncfusion:GridViewDefinition.DataGrid> <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid"> <syncfusion:SfDataGrid.HeaderStyle> <Style TargetType="syncfusion:GridHeaderCellControl"> <Setter Property="Background" Value="Red"/> </Style> </syncfusion:SfDataGrid.HeaderStyle> <syncfusion:SfDataGrid.Columns> <syncfusion:GridTextColumn MappingName="OrderID" /> <syncfusion:GridTextColumn MappingName="ProductID" /> <syncfusion:GridTextColumn MappingName="UnitPrice" /> <syncfusion:GridTextColumn MappingName="Quantity" /> <syncfusion:GridTextColumn MappingName="CustomerID" /> <syncfusion:GridTextColumn MappingName="Discount" /> <syncfusion:GridDateTimeColumn MappingName="OrderDate"/> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid> </syncfusion:GridViewDefinition.DataGrid> </syncfusion:GridViewDefinition> </syncfusion:SfDataGrid.DetailsViewDefinition> <syncfusion:SfDataGrid.Columns> <syncfusion:GridTextColumn MappingName="OrderID" /> <syncfusion:GridTextColumn MappingName="CustomerID" /> <syncfusion:GridDateTimeColumn MappingName="ShippingDate" /> <syncfusion:GridTextColumn MappingName="EmployeeID" /> <syncfusion:GridTextColumn MappingName="Freight" /> <syncfusion:GridCheckBoxColumn MappingName="IsClosed" /> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid> </Grid>
When you set AutoGenerateRelations as “True”, you can customize the DetailsView Grid via the SfDataGrid.AutoGenerateRelations event.
The HeaderStyle of DetailsView is customized when AutoGenerateColumns is set to “True”.
C#
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.dataGrid.AutoGeneratingRelations += dataGrid_AutoGeneratingRelations; } void dataGrid_AutoGeneratingRelations (object sender, Syncfusion.UI.Xaml.Grid.AutoGeneratingRelationsArgs e) { e.GridViewDefinition.DataGrid.HeaderStyle = (Style)this.Resources["headercolor"]; } }
Here, you can see that the Header’s Background color has been changed for DetailsView Grid.
Figure 1: Header Background color changed
Sample Links:
I hope you enjoyed learning about how to change background color of header alone in Grid.
You can refer to our WPF DataGrid featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid demo to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!