Get column index by Column header in WPF DataGrid

后端 未结 2 788
半阙折子戏
半阙折子戏 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 10:53

    Look like "paul" have an small error. Here is my code:

    var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题