Search code examples
c#objectlistview

c# ObjectListView - Not updating sorting after a vlue is changed


I am using ObjectListView to display a dictionary values, it's work perfect but when i sort it sort one time, i mean when i sort by age descending, it's sort them like {30, 27, 26, 15, 10}

here is an example of my object class

public class Item: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    int age = -1;
    public Int32 Age
    {
        get{return age;} 
        set
        {
            if (value == age)
                return;
            this.age = value;
            OnPropertyChanged("Age");
        }
    }
}

Also here is how i append the objects list to the list-view

System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
try
{
    this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
    stopWatch.Start();
    this.listview1.SetObjects(this.ItemsDictionary.Values);
}
finally
{
    stopWatch.Stop();
    this.Cursor = System.Windows.Forms.Cursors.Default;
}

i used

this.listview1.Update();

but i know this is updating painting of the control not the sorting.

If the object of age 15 is updated (increased or decreased) the sorting is not updated and it's still in the same position, What i need to do to make it auto-sort-update when any value of sort column is changed?


Solution

  • i solved it by using this code with each item i update

        this.listview1.Sort(this.listview1.LastSortColumn);
        this.listview1.UpdateObject(OBJ);
    

    Maybe this helps anyone else.