Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:03+00:00 2026-05-27T09:18:03+00:00

I’m having some trouble with my datagrid : When I update some data (from

  • 0

I’m having some trouble with my datagrid :

When I update some data (from the model) it shows on my DataGrid, but if click on a header to sort the column it begins to go sideways when I update existing data.

Here’s 2 1 examples :

  • If I add a new value, it doesn’t appear at the end (like it does when I don’t sort the datagrid) but it shows at the wrong place (same place every time).
  • If I update an existing value, the order never changes when it needs to do so.

I’ve seen multiple answers but some say DataGridTextColumn shouldn’t be a problem… So I was wondering if it was because of the dictionary…
I know that the first problem with new data had something to do with the dictionary.

Object.

    public class Player : INotifyPropertyChanged
{
    private string _id;
    private string _name;
    private int _damage;
    private int _heal;
    private int _dps;
    private int _hps;
    private int _time = 1;               

    public Player(string id, string name)
    {
        _name = name;
        _id = id;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string Id 
    {
        get { return _id; }
        private set
        {
            _id = value;
        }
    }

    public string Name
    {
        get { return _name; }
        private set
        {                
            _name = value;                
            NotifyPropertyChanged("Name");                
        }
    }

    public int Damage
    {
        get { return _damage; }
        set
        {
            _damage = value;                
            NotifyPropertyChanged("Damage");
            Dps = _damage / _time;
        }
    }

    public int Heal
    {
        get { return _heal; }
        set
        {
            _heal = value;
            NotifyPropertyChanged("Heal");
            Hps = _heal / _time;
        }
    }

    public int Dps
    {
        get { return _dps; }
        private set
        {
            _dps = value;
            NotifyPropertyChanged("Dps");
        }
    }

    public int Hps
    {
        get {return _hps; }
        private set
        {
            _hps = value;
            NotifyPropertyChanged("Hps");
        }
    }

    public int Time
    {
        get { return _time; }
        set
        {
            _time = value;
            Dps = _damage / _time;
            Hps = _heal / _time;
        }
    }

    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

}

ObservableCollection

public sealed class ShortList
{
    private static readonly ShortList instance =  new ShortList();
    private ObservableCollection<Player> playerList = new ObservableCollection<Player>();

    private ShortList()
    {
    }

    public static ShortList getShortList
    {
        get
        {
            return instance;
        }
    }

    public ObservableCollection<Player> getPlayerList
    {
        get
        {
            return playerList;
        }
    }

    public void updatePlayer(string id, string name, int damage, int heal, int time) 
    {
        Player player;
        player = findPlayer(id);
        if (player != null)
        {                
            player.Damage = player.Damage + damage;
            player.Heal = player.Heal + heal;
            player.Time = player.Time + time;                
        }
        else
        {                
            player = new Player(id, name);
            player.Damage = damage;
            player.Heal = heal;
            player.Time = time;
            playerList.Add(player);
        }                       
    }

    public void clear()
    {
        playerList.Clear();
    }

    private Player findPlayer(string id)
    {
        foreach (Player p in playerList)
        {
            if (p.Id == id)
            {
                return p;
            }
        }
        return null;
    }

}

XAML

<DataGrid AutoGenerateColumns="False"Name="playerDataGrid" IsReadOnly="True" ItemsSource="{Binding}">
    <DataGrid.Columns>
       <DataGridTextColumn Header="Nom" Binding="{Binding Name}" MinWidth="35"/>
       <DataGridTextColumn Header="Degats" Binding="{Binding Damage}" MinWidth="45" />
       <DataGridTextColumn Header="DPS" Binding="{Binding Dps}" MinWidth="29" />
       <DataGridTextColumn Header="Soins" Binding="{Binding Heal}" MinWidth="35" />
       <DataGridTextColumn Header="HPS" Binding="{Binding Hps}" MinWidth="29" />
   </DataGrid.Columns>
</DataGrid>

Code behind the window

public partial class MiniParser : Window
{
        public MiniParser()
        {            
            InitializeComponent();
            playerDataGrid.ItemsSource = ShortList.getShortList.getPlayerList;
            temporyFill();
        }

        private void temporyFill()
        {
            ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 2);
            ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 0);
            ShortList.getShortList.updatePlayer("123", "ABC", 50, 0, 1);
            ShortList.getShortList.updatePlayer("234", "ABC", 0, 50, 1);
            ShortList.getShortList.updatePlayer("345", "BCD", 1000, 25, 25);
            ShortList.getShortList.updatePlayer("456", "CDE", 250, 0, 25);
        }

        private void startMI_Click(object sender, RoutedEventArgs e)
        {
            ShortList.getShortList.updatePlayer("5678", "BABA", 100, 100, 100);
            ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 0);            
        }
}

Of course most of the code behind is there for testing purpose… But the idea is that the model is being updated and the view needs to reflect the changes (Even the sorting).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:18:04+00:00Added an answer on May 27, 2026 at 9:18 am

    I found a solution, so far so good :

    The problem was that ObservableCollection doesn’t trigger the event “CollectionChanged” when you change the value of a field in an object which is already in your ObservableCollection. (Possibly because you don’t change the reference to it).

    Here’s an example :

    ObservableCollection<MyObject> myCollection = new ObservableCollection<MyObject>();
    myCollection.add("CollectionChanged","Yes");
    //Let's say I had a method to find a object with the first string and to change the second string
    myCollection.change("CollectionChanged", "No");
    

    As you can guess, the second part when I changed a field of my existing object the CollectionChanged didn’t trigger…

    So the solution I implemented is the following :

    class ObsCollection<T> : ObservableCollection<T>
        {
            public void UpdateCollection()
            {
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(
                                NotifyCollectionChangedAction.Reset));
            }
        }
    

    All you have to do is create the new type of collection and then when you change a field value of an existing object, call the UpdateCollection() method.

    This solution is from Wu Xuesong.

    I’d like to say Rachel as been also a big help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.