wpf DataGrid自动显示行号

网上查了很多方法,基本都是自己写一个列,再用程序来填充这列的值为行号,太复杂了。

其实有个简单的思路就可以用3行代码实现这个功能:当DataGrid加载行时,将自身的索引值加1不就是行号了吗?将这个值写入表头(行)就行了。

 
    
dataGrid.LoadingRow += new EventHandler < DataGridRowEventArgs > (dataGrid_LoadingRow);
// 添加行号
public void dataGrid_LoadingRow( object sender, DataGridRowEventArgs e)
{
e.Row.Header
= e.Row.GetIndex() + 1 ;
}

转载于:https://www.cnblogs.com/Laro/archive/2011/04/04/2005490.html

你可能感兴趣的:(wpf DataGrid自动显示行号)