c# WPF DataGrid设置一列自增一
作者:互联网
方法一:
不新增列,利用最左侧列。
首先, 为DataGrid添加LoadingRow事件:
1 this.dataGridEquipment.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.dataGridEquipment_LoadingRow);
LoadingRow实现:
1 private void dataGridEquipment_LoadingRow ( object sender, DataGridRowEventArgs e) 2 { 3 e.Row.Header = e.Row.GetIndex() + 1; 4 }
运行结果如下如所示:
方法二:
新增一列显示序号。
首先,利用DataGridTemplateColumn自定义列:
1 <DataGridTemplateColumn Width="55" Header="序号" > 2 <DataGridTemplateColumn.CellTemplate> 3 <DataTemplate> 4 <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock> 5 </DataTemplate> 6 </DataGridTemplateColumn.CellTemplate> 7 </DataGridTemplateColumn>
然后,为DataGrid添加LoadingRow事件:
1 this.dataGridEquipment.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.dataGridEquipment_LoadingRow);
LoadingRow实现:
1 private void dataGridEquipment_LoadingRow ( object sender, DataGridRowEventArgs e) 2 { 3 e.Row.Header = e.Row.GetIndex() + 1; 4 }
效果如下图所示:
标签:GetIndex,dataGridEquipment,自增,c#,LoadingRow,private,DataGrid,Row 来源: https://www.cnblogs.com/wynblogscc/p/13929981.html