This is a migrated thread and some comments may be shown as answers.

How to programatically sort a RadGridView

0 Answers 149 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rocio
Top achievements
Rank 1
Rocio asked on 29 Aug 2012, 09:34 AM
Hello, I would like to ask you a question, and I hope someone can help me, I have a WPF browser application with a RadGridView to show some data, the data source for it is an Observable collection, so when the collection changes, you can see the changes directly in the grid, but the problem is that when an item changes, it goes to the last row, and what I want to do, is after changing the element in the observable collection, order the radGridView by one of it's columns.

This is my RadGridView, and I want it to be ordered by the "Name" column
<telerik:RadGridView Margin="178,118,0,0" Name="gridAnalog" AutoGenerateColumns="False" ColumnWidth="Auto" telerik:StyleManager.Theme="Vista" CanUserDeleteRows="False" CanUserInsertRows="False" ActionOnLostFocus="None"   Height="Auto" VerticalAlignment="Top" Visibility="Hidden" IsReadOnly="True" HorizontalAlignment="Left" Width="Auto">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Key}" Width="auto"/>
<telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Value.Description}"  Width="auto" />
<telerik:GridViewDataColumn Header="State" DataMemberBinding="{Binding Value.State}"  Width="auto"/>
<telerik:GridViewDataColumn Header="Current value" DataMemberBinding="{Binding Value.Curval}"  Width="auto" />
 </telerik:RadGridView.Columns>
</telerik:RadGridView>

Here I set the ItemsSource property to an ObservableCollection:

gridAnalog.ItemsSource = SummaryDictionary.analogData;

And this is the function that changes the values of the observableCollection, and at the end of this function is where I want to order my Grid:

public static void updateChanges(string key, string value)
{
    //Here I change an entry from the ObservableCollection
    subscriberData[key] = value;
   //And now I want to order
}

I have been searching through your webPage and I found this:
gridAnalog.SortDescriptors.Add(new ColumnSortDescriptor()
 {
        Column = gridAnalog.Columns["Name"],
        SortDirection = ListSortDirection.Ascending
});

so I added this code to the function that modify the content of the observable collection:
public static void updateChanges(string key, string value)
{
    //Here I change an entry from the ObservableCollection
    subscriberData[key] = value;
   //And now I want to order
   gridAnalog.SortDescriptors.Add(new ColumnSortDescriptor()
    {
           Column = gridAnalog.Columns["Name"],
            SortDirection = ListSortDirection.Ascending
    });
}
But it doesn't work,  I suppose I am doing something wrong, because when a value changes, the row goes to the las position on the grid.
Hope someone could help me, because I found a kind of article in your page named: Sorting DataGrid programatically, but the page is not working.
Thanks a lot!

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Rocio
Top achievements
Rank 1
Share this question
or