Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DataGrid: How to display a numbered row index as the first column? #4985

Unanswered
kg-github asked this question in Questions & Help
Discussion options

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?

You must be logged in to vote

Replies: 1 comment

Comment options

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();
 }
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /