Search code examples
c#wpfwpfdatagrid

Add Numeric list column for each row like in excel


enter image description here

How to add a column in wpf datagrid that shows a numeric list for each row like in excel?

enter image description here


Solution

  • Add LoadingRow event to your DataGrid:

    <DataGrid LoadingRow="grd_OnLoadingRow" Name="grd" ... >
    

    Then:

    private void grd_OnLoadingRow(object sender, DataGridRowEventArgs e)
    {
         e.Row.Header = (e.Row.GetIndex()).ToString();
    }
    

    But it start counting from 0 if you want to start from 1 you can try this:

    e.Row.Header = (e.Row.GetIndex() + 1).ToString();