I am dynamically adding sfDataGrids in code behind (required for the application). I have seen the Xaml at https://help.syncfusion.com/wpf/datagrid/styles-and-templates#styling-rowheader.
Is there a way to achieve the same result in C# code behind, given an instance of type SfDataGrid to work with?
(Seems putting the Xaml from the above link onto the wpf page only works with sfDatagrids designed in the Xaml.)
Thanks!
Hi Peter,
You can achieve your requirement to apply the header style through the code behind
by using the below code snippet,
this.dataGrid.Loaded
+= OnLoaded; { Style headerStyle = new Style(); headerStyle.TargetType = typeof(GridHeaderCellControl); Setter setterBackground = new Setter(); setterBackground.Property = BackgroundProperty; setterBackground.Value = Brushes.Cyan; Setter setterBorderThickness = new Setter(); setterBorderThickness.Property = BorderThicknessProperty; setterBorderThickness.Value = new Thickness(1); headerStyle.Setters.Add(setterBackground); headerStyle.Setters.Add(setterBorderThickness); dataGrid.HeaderStyle = headerStyle; } |
We have prepared the sample for your reference, please have a look at this.
Regards
Dhanasekar M.
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Hi,
Many thanks for this! However, your example code appears to style the column headers. What I would like to learn is how to style the row headers with incrementing numeric labels, as in:
I tried adapting your code by changing your code to "headerStyle.TargetType = typeof(GridRowHeaderCell);" but failed to work out which SfDataGrid property to assign it to in the last line of your example code.
Sorry for my slowness! :-) And thanks.
Peter,
We regret to let you know that, there are no possibilities to achieve your requirement to “Show row numbers in the grid, using code behind”
We regret to let you know that, there are no possibilities to achieve your requirement to “Show row numbers in the grid, using code behind”
That's a shame, but thank you for your response. It seems like quite a basic and common requirement that, to my thinking at least, it ought to be available as a property, as in:
mygrid.ShowRowNumbers = true;
Peter,
Currently, we are analyzing your requirement of "Show row
numbers in the grid, using code behind" and update you with further
details on January 13, 2023.
Peter,
On further analysis, Your requirement to “Show row numbers in the grid, using
code behind” will be achievable by using the required XAML stylings in the
App.XAML and add the SfDataGRId in runtime shown below,
App.XAML:
<Application.Resources> <Style TargetType="syncfusion:GridRowHeaderCell"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="syncfusion:GridRowHeaderCell"> <Border x:Name="PART_RowHeaderCellBorder" Background="Bisque" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <!--RowIndex is displayed here --> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding RowIndex, RelativeSource={RelativeSource TemplatedParent}}" TextAlignment="Center" /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Application.Resources> |
Adding the SfDataGrid through the button click:
private void AddGird_Click(object sender, RoutedEventArgs e) { SfDataGrid sfDataGrid = new SfDataGrid(); sfDataGrid.AllowEditing = true; sfDataGrid.AllowFiltering = true; sfDataGrid.AutoGenerateColumns = true; sfDataGrid.ShowRowHeader = true; sfDataGrid.ItemsSource = (this.DataContext as ViewModel).OrdersListDetails; sfDataGrid.ShowGroupDropArea = true; RootGrid.Children.Add(sfDataGrid); } |
We have prepared the sample based on the above suggestion, please have a look at this.
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Attachment: SfDataGrid_Demo__ebb46180.zip
Thanks. The example in the attachment works for me. I've checked out my code and I'm calling this to change the styles to follow my MahApps.Metro dark or light style, and this is causing the grid row numbers to disappear. When I comment out this code, I get the row numbers:
Is there a way to work round this difficulty?
Thanks!
Pete
Peter,
We have considered your requirement “Need to provide support for RowHeaderStyle” and logged a feature request for it. We will update the feedback link and further details for this feature tomorrow (January 18, 2023).
We appreciate your patience until then.
Peter,
As we mentioned earlier, we have analyzed and considered your requirement of “Need to provide support for RowHeaderStyle” and logged a feature request for the same. We will implement this feature in any of our upcoming releases.
At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We will let you know when this feature is implemented. We appreciate your patience until then.
Thank you for requesting this feature and helping us define it. We are always trying to make our products better and feature requests like yours are a key part of our product growth efforts.
Feedback link: https://www.syncfusion.com/feedback/40439/need-to-provide-support-for-rowheaderstyle
If you have any more specifications/suggestions for the feature request, you can add them as a comment in the portal and cast your vote to make it count.