XamDataGrid Column position index: Field.Index or Field.ActualPosition.Column

4.7k Views Asked by At

Let's imagine that grid already bound to a data rows and has multiple columns.

I found that I can retrieve a given column position index by:

var fieldsLayout = grid.FieldLayouts[0];
var columnField = fieldsLayout.Fields.Single(f => f.Name == "Column Name");
int columnIndex = ... see below
  • columnField.Index - If user does not changed an initial columns order
  • columnField.ActualPosition.Column - If user has changed an initial columns order

The question is how to know whether an user has changed initial columns order?

2

There are 2 best solutions below

0
On BEST ANSWER

Whilst investigating I've found that at the initial stage, when columns order has not been changed yet, field.ActivePosition.Column for each columns is 0 or == field.Index, so by introducing following flag:

bool initialOrderChanged = fieldsLayout.Fields.Any(f => 
                                       f.ActualPosition.Column != 0 
                                       && 
                                       f.ActualPosition.Column != f.Index);

I can get right column position order in following way:

 int position = initialOrderChanged
                      ? field.ActualPosition.Column
                      : field.Index,
1
On

Store the initial column list and compare the initial column list with the actual list. If there is any difference, in the order, the column order was changed.