The basic functionality I'm looking for is the ability to select rows together. If I have some ID with duplicates, and I want to select all other rows with the same ID together, how can I do that?
My current attempt involves handling the AfterRowActivate event, I then loop through (I realize this is a bad idea, and I'm going to try to replace it with a dictionary to get O(1) lookup time) and find all the rows that match.
AfterRowActivate
I've tried toggling Row.Selected and/or adding those rows to the UltraGrid.Selected.Rows collection, but as soon as those rows are selected, they are deselected again (you can visually see this happen).
Row.Selected
UltraGrid.Selected.Rows
CellClickAction is set to RowSelect.
CellClickAction
RowSelect
I've attached a sample project, try selecting one of rows with "Bob" for first name. For me, this causes the other row with "Bob" to get selected momentarily, then immediately deselect. Perhaps I'm handling the wrong event? Thanks y'all!
Edit: Since rows selected together should probably also be deselcted together, should I really be handling the AfterSelectChange event?
AfterSelectChange
Handling the AfterSelectChange event was the right way to go.
Attached is the code that works for me. Note that the logic in the event handler is O(n^2), so if you have lots of rows, that is a terrible way to do it. Replacing the inner loop with a dictionary would be one way to handle that.
If you are concerned about efficiency and you don't want to build a dictionary, you can make the code more efficient in a number of ways. Here's a sample:
Private Sub UltraGrid1_AfterSelectChange(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs) Handles UltraGrid1.AfterSelectChange Dim selectedKeys As List(Of String) = New List(Of String)() ' Build a list of unique keys. This way we don't have to search the entire grid 'for the same key twice. For Each selRow In UltraGrid1.Selected.Rows Dim key As String = selRow.Cells("First").Text If (False = selectedKeys.Contains(key)) Then selectedKeys.Add(key) End If Next ' Build the list of the new selected rows without selecting anything. Dim newSelectedRows As List(Of UltraGridRow) = New List(Of UltraGridRow)() For Each r In UltraGrid1.Rows.GetFilteredInNonGroupByRows() Dim key As String = r.Cells("First").Text If (selectedKeys.Contains(key)) Then newSelectedRows.Add(r) End If Next ' Disable this event to prevent recursion. Me.UltraGrid1.EventManager.SetEnabled(GridEventIds.AfterSelectChange, False) Try ' Select all of the rows in one operation Me.UltraGrid1.Selected.Rows.AddRange(newSelectedRows.ToArray()) Finally ' Re-enable the event. Me.UltraGrid1.EventManager.SetEnabled(GridEventIds.AfterSelectChange, True) End Try End Sub