Compare cell object with Spread.ActiveSheet.ActiveCell

Posted by: shinsid on 8 September 2017, 6:20 am EST

    • Post Options:
    • Link

    Posted 8 September 2017, 6:20 am EST

    Hello,

    I have a cell object which I want to compare with the active cell of the spreadsheet to check if its the same cell.

    How to to this?

    Cell myCellObject = fpSpreadActiveSheet.Cells[ 1 , 1 ];
    
    if(fpSpread.ActiveSheet.ActiveCell == myCellObject)
    {
    
    }
    

    The above code doesn’t do the comparison successfully. And I dont want to use the addresses of the cells to compare with the Active cell address.

    I want to compare the object of my cell with the object of Active Cell of spread.

    Any help will be appreciated.

    Thanks in advance.

    Best regards,

    Siddharth

  • Posted 8 September 2017, 6:20 am EST

    Hi Siddharth,

    For this you could use the Equals method of the Cell class: http://sphelp.grapecity.com/WebHelp/SpreadNet8/WF/webframe.html#FarPoint.Win.Spread~FarPoint.Win.Spread.Cell~Equals.html

    For your code above you would use this instead:

    [csharp]Cell myCellObject = fpSpreadActiveSheet.Cells[ 1 , 1 ];

    if(fpSpread.ActiveSheet.ActiveCell.Equals(myCellObject))

    {

    }[/csharp]

    Let me know if that is what you are looking for.

    Regards,

    Kevin

  • Posted 8 September 2017, 6:20 am EST

    Hi Kevin,

    I would also like to search for its corresponding value in a dictionary by passing cell as a key. Can you give some idea how to do it?

    Dictionary dic = new Dictionary();
    
    string s;
    dic.TryGetValue(fpSpread1.ActiveSheet.ActiveCell, out s);
    

    Regards,

    Siddharth

  • Posted 8 September 2017, 6:20 am EST

    Sorry there was a mistake in the above code… The dictionary is like this…

    “Dictionary{Cell, string} dicOfCells = new Dictionary{Cell, string}();”

    Sorry I am using {} instead of < & as its not allowing.

  • Posted 8 September 2017, 6:20 am EST

    Hi Siddharth,

    That would be the way to pass in a cell as a key to the dictionary. You could have it be related to any value in the dictionary (string in this case).

    Regards,

    Kevin

  • Posted 8 September 2017, 6:20 am EST

    I didn’t understand your reply. Can you please elaborate?

    And the problem is that I am not able to find the cell in the dictionary; even if its present in the dictionary.

    Best regards,

    Siddharth

  • Posted 8 September 2017, 6:20 am EST

    Hi Siddharth,

    I’m now noticing that problem. It should work to get the value like that, but it appears that the implementation of GetHashCode in the Cell class is incorrect, and doesn’t require that GetHashCode returns the same value for different instances of an object representing the same thing. This makes it seem like it can’t find the cell in the dictionary, even though it has been added in.

    One way to workaround this is to use the same instance of the cell to compare against:

    [csharp]Dictionary<Cell, string> dic = new Dictionary<Cell, string>();

    Cell testCell = fpSpread1.ActiveSheet.Cells[0, 1], “1”);

    dic.Add(testCell, “1”);

    string s;

    dic.TryGetValue(testCell, out s);[/csharp]

    In this example, “s” would be “1”.

    Let me know if that helps.

    Regards,

    Kevin

  • Posted 8 September 2017, 6:20 am EST

    Hi Siddharth,

    The developers have shared a better workaround that doesn’t require keeping the old instance:

    [csharp]public class CellEqualityComparer : IEqualityComparer

    {

    public bool Equals(Cell x, Cell y)

    {

    return (x == null && y == null) || (x != null && x.Equals(y));

    }

    public int GetHashCode(Cell obj)

    {

    return obj.Row.Index ^ obj.Column.Index;

    }

    }

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

    dic.Add(fpSpread1.ActiveSheet.Cells[0, 1], “1”);

    dic.Add(fpSpread1.ActiveSheet.Cells[0, 2], “2”);

    string s;

    dic.TryGetValue(fpSpread1.ActiveSheet.Cells[0, 1], out s);[/csharp]

    Let me know if that helps.

    Regards,

    Kevin

  • Posted 8 September 2017, 6:20 am EST

    Hello Kevin,

    The solution given by your developer team seems to be be incorrect in the situation where the dictionary contains cells from different sheets.

    dic.Add(fpSpread1_Sheet1.Cells[1, 1], fpSpread1_Sheet1.Cells[1, 1].Text);
                dic.Add(fpSpread1_Sheet1.Cells[2, 2], fpSpread1_Sheet1.Cells[2, 2].Text);
                dic.Add(fpSpread1_Sheet2.Cells[1, 1], fpSpread1_Sheet2.Cells[1, 1].Text);
                dic.Add(fpSpread1_Sheet2.Cells[2, 2], fpSpread1_Sheet2.Cells[2, 2].Text);
    

    Best regards,

    Siddharth Shinde

  • Posted 8 September 2017, 6:20 am EST

    Hi Siddharth,

    I was unable to reproduce the problem using the following code:

    fpSpread1.Sheets.Add(new FarPoint.Win.Spread.SheetView());
    
    fpSpread1.Sheets[0].Cells[1, 1].Text = "Cell1";
    fpSpread1.Sheets[0].Cells[2, 2].Text = "Cell2";
    fpSpread1.Sheets[1].Cells[1, 1].Text = "Cell3";
    fpSpread1.Sheets[1].Cells[2, 2].Text = "Cell4";
    
    Dictionary<Cell, string> dic = new Dictionary<Cell, string>(new CellEqualityComparer());
    
    dic.Add(fpSpread1.Sheets[0].Cells[1, 1], fpSpread1.Sheets[0].Cells[1, 1].Text);
    dic.Add(fpSpread1.Sheets[0].Cells[2, 2], fpSpread1.Sheets[0].Cells[2, 2].Text);
    dic.Add(fpSpread1.Sheets[1].Cells[1, 1], fpSpread1.Sheets[1].Cells[1, 1].Text);
    dic.Add(fpSpread1.Sheets[1].Cells[2, 2], fpSpread1.Sheets[1].Cells[2, 2].Text);
                
    string s;
    dic.TryGetValue(fpSpread1.Sheets[1].Cells[2, 2], out s);
    MessageBox.Show(s);
    

    When I ran that code, it correctly showed “Cell4” in the message box that popped up. Also, if it makes any difference, I had the code for the CellEqualityComparer class in the same namespace but different class as my form like so:

    namespace WinForms_C_Sharp_Test
    {
        public class CellEqualityComparer : IEqualityComparer<Cell>
        {
            public bool Equals(Cell x, Cell y)
            {
                return (x == null && y == null) || (x != null && x.Equals(y));
            }
            public int GetHashCode(Cell obj)
            {
                return obj.Row.Index ^ obj.Column.Index;
            }
        }
    
        public partial class Form1 : Form
        { ....etc....
    

    Let me know if that helps.

    Regards,

    Kevin

  • Posted 8 September 2017, 6:20 am EST

    Hello Kevin,

    This looks good. CHEERS !!

    Thanks a lot for your support. I’ll soon come up with few more challenges for you and your team. :wink:

    I wish you a great day !

    Best regards,

    Siddharth

  • Posted 8 September 2017, 6:20 am EST

    Hello,

    We are glad to know that your issue is resolved.

    Thanks,

    Reeva

  • Posted 12 October 2017, 10:05 pm EST

    Hi,

    But this will fail if sorting in there in the spread. For example, I am adding cells to the Dictionary while loading spread. After that if I sort the spread, it is giving a different cell. It compares only the row index and the column index. Not the cell object. Is there any workaround for this scenario? I guess the GetHashCode logic needs to be changed. Please reply.

  • Posted 17 October 2017, 12:50 am EST

Need extra support?

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

Learn More

Forum Channels