Hi,
Is there any way to change the field position by code in the field layout?.
The ActualPosition property is read only and changing the Column property has not effect... I guess that the ActualPosition property as priority over the Column property.
Anyone?
Thanks.
Hello sebl,
The FieldCollection object inherits an ObservableCollection<Field>. You can utilize the ObservableCollection's Move() method to accomplish what you are looking for. For example:
grid.FieldLayouts[0].Fields.Move(1, 0);
The two integer parameters are the oldIndex and newIndex, respectively.
For more information, you can see the documentation here.
OK,
This does allow me to change the index of the field, but the property ActualPosition is still the one that seems to be used when displaying the fields.
In the end, all fields remain at their current position.
My issue was programatically moving columns (via a custom field selector GUI) + allowing user to do drag&drop was not working together. In otherwords after moving some columns and drag-drop of some other columns, doing SaveCustomization does not regain correct position.
Please let me know you described this as fixed in 2010.03.
Thanks,
Muthu
Hello Muthu,
I have attached a modified version of the sample I attached the last time. It has the FieldLayoutSettings.AllowFieldMoving property set to WithinLogicalRow so you can grad column headers and still keep them next to each other without having them stack vertically. You can change this property a different value and it will still work, but it's easier to demonstrate if the column headers won't stack.
When you run the sample, re-arrange the column headers and then click the button to move the name column. It will put it all the way over the the right. Then, click the Save Customizations button, stop the application and run it again and click the Load Customizations button. It will arrange the columns in the order in which they were last saved.
Please let me know if you have any questions.
Sincerely,
CharlieSenior Developer Support EngineerMCTS
Great. Save and load customization after drag/drop + moving fileds programatically works.
One thing I noticed though. The FieldPosition.Column - it is zero based index or 1? Looks like this is impacting the actual position. In your sample, you have 4 columns and you are moving the first column to last by setting the column index as 4, so it is not zero based?
In your sample, (1)move the Salary column as 2nd column (name, salary...) and now actual position of Salary column is 1 (2)now click on move field (3) the actual position of salary column still show it as 1 eventhough it is first column in grid.
If you save this and load. It show the columns in order however the actual position doesn't appear to be corrrect.
Is this expected?
Hi, any update on this, is the index is zero based or one based?
Hello Muthu.
Yes, you are correct. The FieldPosition is not zero based; it starts at 1. Field indexes within the FieldLayoutsCollection are zero based, but the FieldPositions are not.
Hi Charlie, Ok, Thanks for clarifying.
The grid is functioning as designed. To programmatically move a column in the collection and then have the UI reflect that change you need to create a new FieldPosition object and pass in the new positions.
There is an overload on the FieldPosition constructor that takes 4 integers: int column, int row, int columnSpan, int rowSpan. The first parameter (the column) is not zero based. The values that you pass into this constructor are positions in a layout manager; they are not actual positions in an ObservableCollection, which is what the Field.Index is. The FieldsCollection derives from ObservableCollection so Field.Index is the zero based position of the Field object in the collection.
However, positions in a layout manager are different; they are not zero based. If the fields[0] is already at column 0 and then the next 3 fields are at column 1, 2 and 3 respectively, you will have to set the column to 4 to make sure it's after the last one. This is what these lines of code will do for the sample I attached to my last response:
fields[0].ActualPosition = new FieldPosition(4, 0, 1, 1);this.xamDataGrid1.FieldLayouts[0].EnsureUniqueFieldPositions();
That code will move the first field in FieldLayout[0] to the last position. Please let me know if you have any questions.
CharlieSenior Developer Support Engineer MCTS
Is this function as designed or is a Bug?
But it looks like it is ignoring the first column for some reason, I noticed the same behavior for the Move method as well and reported as a separate issue..