When the user right clicks on a row, I would like for that row to become the selected row as if the user left clicked the row, for the purpose that the context menu will display options relevant to the selected row.
I have the grid set to single row select by setting:
UltraGrid.DisplayLayout.Override.SelectTypeRow = SelectType.Single;
In order to select the row on a right mouse click I used the MouseDown event:
private void UltraGrid_MouseDown(object sender, MouseEventArgs e){ UltraGridRow row; UIElement element;
if (e.Button == MouseButtons.Right) { element = UltraGrid.DisplayLayout.UIElement.ElementFromPoint(e.Location); row = element.GetContext(typeof(UltraGridRow)) as UltraGridRow; if (row != null && row.IsDataRow) { UltraGrid.ActiveRow = row; } }}
The problem is that sometimes, but not always, the previously selected row stays highlighted seemingly to not unselect, and there are then 2 rows highlighted in the grid. Then once this state starts happening, I can't escape it, there are always 2 rows highlighted. If I sutdown the app and restart, it works fine for awhile but then eventually reverts to its problem state.
Is there a step that I'm missing, or is there an easier way to accomplish what I want?
Thanks!
Bill
Maybe setting the ActiveRow causes the problem? Why not taking the right clicked row from grid.Selected.Rows[0] in the BeforeToolDropdown event?
The problem is that grid.Selected.Rows[0] is the wrong row, it is the old row not the new one. So I am putting myself in a state where the Selected Row and the Active Row are different.
The good news is the by calling grid.Selected.Rows.Clear() before setting the Active seems to solve my problem.
If there is a better way I should have done this, I'd be happy to hear it, but thanks for the help.
Hi,
You appear to be mixing up Selected and Active. These are two different thing. Setting the ActiveRow does not affect the Selected Rows and vice versa.
You probably want to set the ActiveRow and then also set the Selected property on the row to true to make it selected.
If you are using single-selection, anyway, then you might want to consider turning off selection entirely and then always using the ActiveRow - just to simplify things.
The selected row is the old one since you're checking it in the mousedown event. Try to use the event that occurs before the menu pop up like BeforeToolDropDown, or use the mouseup event.