Kishor
Kishor

Reputation: 4096

Get column index by Column header in WPF DataGrid

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

I know the Column Header, want to get column index.

Upvotes: 7

Views: 16171

Answers (2)

paul
paul

Reputation: 22001

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

Upvotes: 11

AnHX
AnHX

Reputation: 41

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

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

Upvotes: 4

Related Questions