Does anyone know how can I find out what the child rows are (or their parent row) when the row selector element in a group is clicked? I have used the AfterRowExpanded event successfully to get the parent row, but if several groups are already expanded and the user returns to a previously expanded group to click the row selector, I don't then parent row is the wrong one.
(The reason for this is that I want to select all of the rows within the OutlookGroup with one mouse-click for subsequent copy/paste/Excel export/etc.)
A screen-shot is attached.
Thanks in advance,
Richard
private void ultraGrid1_MouseUp(object sender, MouseEventArgs e) { // Get the grid. UltraGrid grid = (UltraGrid)sender; // Get the last element entered by the mouse. UIElement element = grid.DisplayLayout.UIElement.LastElementEntered; // See if the element is a RowSelectorHeaderUIElement. RowSelectorHeaderUIElement rowSelectorHeaderUIElement = element as RowSelectorHeaderUIElement; // If it's not, see if one of it's ancestors is. if (rowSelectorHeaderUIElement == null) rowSelectorHeaderUIElement = element.GetAncestor(typeof(RowSelectorHeaderUIElement)) as RowSelectorHeaderUIElement; // If we still haven't found a RowSelectorHeaderUIElement by now, just exit. if (rowSelectorHeaderUIElement == null) return; // Get hte rows associated with the RowSelectorHeaderUIElement RowsCollection rows = rowSelectorHeaderUIElement.GetContext(typeof(RowsCollection)) as RowsCollection; // Clear any existing selection. grid.Selected.Rows.Clear(); // If there are rows in the list, select them all. if (rows != null ) { UltraGridRow[] rowsArray = (UltraGridRow[])rows.All; grid.Selected.Rows.AddRange(rowsArray); } }
Thanks for the quick response Mike.
My C# isn't very good. Can you please do a VB.net version instead as my translation doesn't seem to work. eg I'm not sure what the following is trying to do:
RowSelectorHeaderUIElement rowSelectorHeaderUIElement = element as RowSelectorHeaderUIElement;
Thanks,
Hi Richard,
The "as" keyword in C# basically casts a variable into another data type. The great thing about it is that if it fails to cast, it just returns null (Nothing), rather than raising an exception. The closest equivalents in VB that I am aware of are the CType or DirectCast functions, both of which will raise an exception if they fail to perform the type cast. So in VB, you have to jump through some hoops.
My VB is a little rusty, but this seems to work.
Private Sub UltraGrid1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles UltraGrid1.MouseUp ' Get the grid. Dim grid As UltraGrid = CType(sender, UltraGrid) ' Get the last element entered by the mouse. Dim element As UIElement = grid.DisplayLayout.UIElement.LastElementEntered ' See if the element is a RowSelectorHeaderUIElement. Dim rowSelectorHeaderUIElement As RowSelectorHeaderUIElement = Nothing If TypeOf element Is RowSelectorHeaderUIElement Then rowSelectorHeaderUIElement = CType(element, RowSelectorHeaderUIElement) End If ' If it's not, see if one of it's ancestors is. If rowSelectorHeaderUIElement Is Nothing Then element = element.GetAncestor(GetType(RowSelectorHeaderUIElement)) If Not element Is Nothing Then rowSelectorHeaderUIElement = CType(element, RowSelectorHeaderUIElement) End If End If ' If we still haven't found a RowSelectorHeaderUIElement by now, just exit. If rowSelectorHeaderUIElement Is Nothing Then Return End If ' Get the rows associated with the RowSelectorHeaderUIElement Dim context As Object = rowSelectorHeaderUIElement.GetContext(GetType(RowsCollection)) Dim rows As RowsCollection = Nothing If Not context Is Nothing Then rows = CType(context, RowsCollection) End If ' Clear any existing selection. grid.Selected.Rows.Clear() ' If there are rows in the list, select them all. If Not rows Is Nothing Then Dim rowsArray As UltraGridRow() = CType(rows.All, UltraGridRow()) grid.Selected.Rows.AddRange(rowsArray) End If End Sub
Excellent Mike, it's all working now.
Thanks very much.