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;
}
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!