Skip to content

UltraGrid find and activate a row

New Discussion
Paul Syvret
Paul Syvret asked on Feb 10, 2020 3:14 PM

Hi

I have a grid and I refresh the grid after the user has done something to a record.

I want to activate the row the user was on. I know the ID of the row I am just having difficulty finding the correct expression to locate the row in the grid and then activate it.

I am currently trying

UltraGridRow[] rows = this.gridComponents.Grid.Rows.Where(s => s.Cells[“ID”].Value == rowID).ToArray()

then if that had worked I would have done

rows[0].Activate()

but no rows are returned so rows[0].Activate() obviously fails. The row IDs are the same as before the data was refreshed, I can see the row in the watch window, i can also see it in the grid after the refresh so how do I retrieve the row in order to activate it?

Thanks

Paul

Sign In to post a reply

Replies

  • 0
    Paul Syvret
    Paul Syvret answered on Feb 10, 2020 2:50 PM

    Well I have answered it myself.

    The cell value was an object so I did not see a problem with 

    object rowID = ….Cells[“ID”].Value

    and then searching using 

    UltraGridRow[] rows = this.gridComponents.Grid.Rows.Where(s => s.Cells[“ID”].Value == rowID).ToArray()

    nope, I had to convert my cell value to an int, alhtough i probably only had to do it in the search but

    int rowId = COnvert.ToInt32(….Cells[“ID”].Value)

    UltraGridRow[] rows = this.gridComponents.Grid.Rows.Where(s => Convert.ToInt32(s.Cells[“ID”].Value) == rowID).ToArray()

    I suspect I could have left rowID as an object and done

    UltraGridRow[] rows = this.gridComponents.Grid.Rows.Where(s => Convert.ToInt32(s.Cells[“ID”].Value) == Convert.ToInt32(rowID)).ToArray()

    but I will just leave it be now

    Maybe someone can explain why that was?

    Thanks

    Paul

    • 0
      Mike Saltzman
      Mike Saltzman answered on Feb 10, 2020 2:59 PM

      Hi Paul, 

      I just tried this out and I encountered a similiar problem. But in my code, I defined rowID as an int, and so the code would not even compile, since you cannot compare an object to an int. So I changed the code to cast the cell value to an int and then it worked fine. 

      If I declare rowID as an object, it does not find a match.

      The equals operator on type Object is doing a reference comparison, not a value comparison. So one instance of (object)n does not equal another instance of an object even if they have the same numeric value. 

      You can see demonstrate this like so: 

      object a = 55;
      object b = 55;
      Debug.WriteLine(a == b);
      Debug.WriteLine(a.Equals(b));

      This code returns:

      False

      True

      Because the equals operator does a reference comparison. But the Equals METHOD will compare values. So if you don't want to change data types, another workaround for you would be to use the Equals method. 

      UltraGridRow[] rows = this.gridComponents.Grid.Rows.Where(s => s.Cells[“ID”].Value.Equals(rowID)).ToArray()

      • 0
        Paul Syvret
        Paul Syvret answered on Feb 10, 2020 3:10 PM

        Thanks Mike. 

        Much appreciated,

        Paul

      • 0
        Mike Saltzman
        Mike Saltzman answered on Feb 10, 2020 3:14 PM

        No Problem. I've been tripped up by the object equals operator a few times, myself. 

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Paul Syvret
Favorites
0
Replies
4
Created On
Feb 10, 2020
Last Post
6 years, 8 months ago

Suggested Discussions

Tags

Created by

Created on

Feb 10, 2020 3:14 PM

Last activity on

Feb 23, 2026 10:29 AM