在WPF中更新DataGrid中的行号可以通过以下步骤实现:
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column.DisplayIndex == 0)
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = "行号";
column.Binding = new Binding("DataGridRow.Header");
e.Column = column;
}
}
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
<DataGrid x:Name="dataGrid" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" LoadingRow="DataGrid_LoadingRow">
<!-- DataGrid的其他设置 -->
</DataGrid>
这样,当DataGrid加载数据时,每一行都会显示对应的行号。
领取专属 10元无门槛券
手把手带您无忧上云