WPF DataGrid添加编号列?

第一步:
<DataGridTemplateColumn Header="编号" Width="50" MinWidth="10" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" 
HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn>


第二步:添加DateGrid LoadingRow 事件,如下所示:
private void dgvList_LoadingRow(object sender, DataGridRowEventArgs e)
{
  e.Row.Header = e.Row.GetIndex() + 1;
}

相关文章:

  • 2021-07-27
  • 2021-09-07
  • 2021-08-15
  • 2022-12-23
  • 2021-07-15
  • 2022-03-04
  • 2021-06-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-12-28
  • 2021-07-15
  • 2022-01-15
相关资源
相似解决方案