Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column index by Column header in WPF DataGrid

Tags:

c#

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.

like image 240
Kishor Avatar asked Oct 25 '12 14:10

Kishor


2 Answers

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

like image 166
paul Avatar answered Oct 14 '22 21:10

paul


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

var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;
like image 26
AnHX Avatar answered Oct 14 '22 20:10

AnHX