Hello,
after I refresh my grid (resetting datasource and putting the new oen), I want to preserve the row selection.
How can i find a particular row from the datasource and make it the activerow,
tx
Marc
Hi Marc,
If you are causing the data source to send a Reset notification, then all rows in the grid are destroyed and new ones are created for the new data.
To find a row, you will need to loop through the rows in the grid, there's no built-in search functionality in the grid. If you know the position of the row you want in the BindingManager, then you can use that index to find the row in the grid using something like this:
this.ultraGrid1.Rows.GetRowWithListIndex(index);
I have the same scenario here, is it not possible to use the ultragrid1.rows.all.find() function to return the row? since we're resetting the grid, we can capture the field value from the grid prior to the reset.
Theres always the old fallback of looping through each row in the grid, but the Find method seems to be what we're looking for here, isnt it?
Um, I don't think so. I could be wrong, but I am pretty sure that the All property is a property on Collection and it just returns an Object array. I wasn't aware that there was a Find method on an Array, but even if there is, I assume it finds some object that you specify. I don't see how it would be able to find an UltraGridRow with a cell that has a particular value. It knows nothing about the grid or cells.
emz,
Thanks for that bit of code and the explanation, it's quite thorough. Got some questions for you: (1) is that really any faster than a foreach loop? According to the MSDN documentation the List<T>.Find method is linear in its search, so how does the optimized CLR achieve find the item any faster? It has to go through all the same items one by one starting at the beginning, just like a foreach loop, and it has to run the same comparison you would use in a foreach loop (row.Cells["ColumnKey"].Text.Equals("Stuff"), for example).
Question (2): Have you considered just using the Single method? Here is what I use in my code:
grid.Rows.Single(row => row.GetCellValue("ColumnKey").ToString() == valueToFind.ToString()).Selected = true;
While Single doesn't work when more than one element in the source with the correct value exists, I assume since we're trying to select a single row, it's going to be off a unique column. (We could use Where otherwise).
I'm very new to LINQ, so this is still a learning process for me and I would appreciate any input.
Thanks,
J
Although this thread looks a bit old I thought I would show my approach which seems to be working well:
gridBindingSource.Position = gridBindingSource.Find("ColumnName", SearchValue))
emz said: // get the row from the grid which represents the column for which the filter was modified. UltraGridRow colRow = this.ActiveGrid.Rows.OfType<UltraGridRow>().ToList().Find(row => row.Cells[0].Text.Equals(valueListColumnFiltersChangedEventArgs.Column.Name, StringComparison.CurrentCultureIgnoreCase));
I am using VB.NET and came up with this piece of code:
Dim rowFinder As RowFinder = New RowFinder(<UltraWebGrid instance here>.Rows.OfType(Of Infragistics.WebUI.UltraWebGrid.UltraGridRow)().ToList(), <ItemId here>)
Dim selectedRow As Infragistics.WebUI.UltraWebGrid.UltraGridRow = rowFinder.RetVal
Public Class RowFinder
Public RetVal As Infragistics.WebUI.UltraWebGrid.UltraGridRow
Private _itemIdToFind As String
Public Sub New(ByVal listRows As List(Of Infragistics.WebUI.UltraWebGrid.UltraGridRow), ByVal itemIdToFind As String)
_itemIdToFind = itemIdToFind
RetVal = listRows.Find(AddressOf CompareItemID)
End Sub
Private Function CompareItemID(ByVal row As Infragistics.WebUI.UltraWebGrid.UltraGridRow) As Boolean
Return CStr(row.Cells.FromKey("ItemID").Value) = _itemIdToFind
End Function
End Class
Apparently VB.NET doesn't support anonymous delegates. See this thread.
Thanks Mike - that's a great method - not sure when it was added, but I had totally missed it.
grid.Rows.GetAllFilteredInNonGroupByRows