Hi guys,
I have a xamGrid in which sorting is enabled.
I insert a new row at index 0 as follows:
myXamGrid.Rows.Insert(0, SomeRow);
Is there any possibility to keep the row at index 0 at the moment it is inserted, without being automatically placed in the position given by sorting?
So, the problem is: I insert the row at position 0 as shown above, but it will placed automatically in another position, given by sorting. I want to keep the row at position 0.
Alin
Hi Alin,
The button click was just for demonstration purposes, in reality it doesn't matter where you call the insert from.
But, as far as i can think of, the only way to prevent the sorting, is to not have the properties of your data object set until after the row Added event, and that even assumes that the default values will be at the top of the sort order.
So unfortunately there isn't another way to stop the sort from occurring, when you add a new item.
Out of curiosity, what exactly are you trying to accomplish? Could you just use the AddNewRow?
-SteveZ
Hi Steve,
I have hundreds of models in my project, each of them have different properties; I don't know the name of the property and the type of the model, so I can not use :
((YourData)e.Row.Data).ChapterNumber = 2;
And, also, I don't want to do this explicitly, by being handled in the handler of another control, such that in the example with the button you provided me.
By taking into consideration these aspects, can I do some workaround or not?
When the row is added, we specifically listen for the collection to change, and when it does, we invalidate sorting so its up to date.
If you really wanted to get around it, you could write a little code, and defer setting the properties values. What i mean is, you can listen to the RowAdded event. And in that event setup the values for your row;
In this example, assume that ChapterNumber is what the xamGrid is currently sorted on. B/c i delay setting it until the rowadded event, it's able to go into the inserted location.
void grid_RowAdded(object sender, RowEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
this.grid.Rows.Insert(0, this.grid.Rows.CreateItem(new YourData(){ ChapterNumber = -1, Title = "INseted ROw"}));