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.
Any update on nmuthu's questions? I do not use customization but I have the similar problems with Field.ActualPosition.Column values. It has the value that does not correspond to real UI visible position after moving columns via drag&dropping.
Thanks, Alex.
I am now using 2010.03 for a different project and just wondering is this fix is already in this version?
Thanks,
Muthu
Hello Muthu.
As of 2009 volume 2, the Field.ActualPosition property is no longer read-only. You can set it. The FieldLayout.EnsureUniqueFieldPositions() method is present also. Here is what you would need to do to swap the position if a field at run time:
FieldCollection fields = xamDataGrid1.FieldLayouts[0].Fields;fields[0].ActualPosition = new FieldPosition(4, 0, 1, 1);this.xamDataGrid1.FieldLayouts[0].EnsureUniqueFieldPositions();
I have attached a sample written in C# using 2010 volume 3 which demonstrates this. Just run the sample, click the button, and it will change the position of the 'name' column from zero to 4. Please let me know if you have any questions.
Sincerely,
CharlieSenior Developer Support EngineerMCTS
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.
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.
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..
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, any update on this, is the index is zero based or one based?