-
Notifications
You must be signed in to change notification settings - Fork 1.4k
DataGrid: How to display a numbered row index as the first column? #4985
Unanswered
kg-github
asked this question in
Questions & Help
-
I thought this would be an easy one but there doesn't seem to be a built-in way to display a row index as the first column of a DataGrid. I have a simple collection of items that I would like to display row numbers for (starting at 1). Anyone have a suggestion?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
You can use the LoadingRow event to set row headers in code-behind and the Primitives package to customize the RowHeader background.
<controls:DataGrid
xmlns:wctprimitives="using:CommunityToolkit.WinUI.UI.Controls.Primitives"
x:Name="MyDataGrid"
HeadersVisibility="All"
LoadingRow="MyDataGrid_LoadingRow"
RowHeaderWidth="50">
<controls:DataGrid.RowHeaderStyle>
<Style TargetType="wctprimitives:DataGridRowHeader">
<Setter Property="Background" Value="Transparent" />
</Style>
</controls:DataGrid.RowHeaderStyle>
<controls:DataGrid.Resources>
<SolidColorBrush x:Key="DataGridColumnHeaderBackgroundColor" Color="Transparent" />
</controls:DataGrid.Resources>
<controls:DataGrid.Columns>
<!-- Columns -->
</controls:DataGrid.Columns>
</controls:DataGrid>
private void ProductsDataGrid_LoadingRow(object sender, CommunityToolkit.WinUI.UI.Controls.DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment