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
No Problem. I've been tripped up by the object equals operator a few times, myself.
Thanks Mike.
Much appreciated,
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()
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
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?