Get column index by Column header in WPF DataGrid

后端 未结 2 808
半阙折子戏
半阙折子戏 2021-01-12 10:26

How to get the Column index or Column.DisplayIndex from its Column Header in WPF DataGrid?

I know th

2条回答
  •  [愿得一人]
    2021-01-12 11:16

    you could use DisplayIndex (be aware this will change if columns are resorted)

    var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;
    

    edited: thanks for suggestion from @AnHX

提交回复
热议问题