Comparing cells using IEqualityComparer

Posted by: ajithedasseril on 15 October 2017, 4:08 pm EST

    • Post Options:
    • Link

    Posted 15 October 2017, 4:08 pm EST

    Hi,

    I have few spreads in my application. I want to add some extra info in each cell. Since the tag property is already used for some other purpose, I created 2 extensions methods for setting and getting the extra info. I am using a dictionary object to store the cells and the extra info (using cell as the key).

    Below is the comparer class I used for the dictionary.

    public class CellComparer : IEqualityComparer<Cell>
        {
            public bool Equals(Cell x, Cell y)
            {
                //return x.Equals(y);
                return (x == null && y == null) || (x != null && x.Equals(y));
            }
    
            public int GetHashCode(Cell obj)
            {
                //int hCode = obj.Column.Index ^ obj.Row.Index;
                //return hCode.GetHashCode();
                return obj.Row.Index ^ obj.Column.Index;
            }
        }
    

    This code works as expected if there is no sorting implemented on the spread. But if we sort the spread, the comparer returns the wrong cell. Any idea how can we resolve this issue?

  • Posted 16 October 2017, 10:32 pm EST

    Hello,

    This is correct behavior, dictionary object referring to a cell which does not have any value after sort or has an incorrect value.

    I could not a way to get the old value from the Key(Cell Object) in your Dictionary object. I have further escalated it to the development team to know if there is a workaround available.

    The tracking id for this issue is:247584

    Thanks,

    Deepak Sharma

  • Posted 18 October 2017, 5:22 pm EST

    Hello Deepak,

    How can I track this issue using the ID?

  • Posted 22 October 2017, 8:53 pm EST

    Hello ,

    Spread’s view model is changed when you apply sort while the Data model remains intact. You can use the code as follows to get the cell value from Dictionary object:

    Dictionary<Cell, string> dic = new Dictionary<Cell, string>(new CellEqualityComparer());

            dic.Add(fpSpread1.Sheets[0].Cells[1, 1], fpSpread1.Sheets[0].Models.Data.GetValue(1, 1).ToString());
            dic.Add(fpSpread1.Sheets[0].Cells[2, 2], fpSpread1.Sheets[0].Models.Data.GetValue(2, 2).ToString());
            dic.Add(fpSpread1.Sheets[1].Cells[1, 1], fpSpread1.Sheets[1].Models.Data.GetValue(1, 1).ToString());
            dic.Add(fpSpread1.Sheets[1].Cells[2, 2], fpSpread1.Sheets[1].Models.Data.GetValue(2, 2).ToString());
    
            string s;
            dic.TryGetValue(fpSpread1.Sheets[1].Cells[2, 2], out s);
            MessageBox.Show(s);
    

    Thanks,

    Deepak Sharma

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels