Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1015
Selecting and Unselecting a Row
posted
Can one select and then unselect a row without clicking on another row in between? I use the row selector to select a row (ie I turn it green to indicate it has been selected) and unselect it (ie if it's green I turn it back to white to indicate it has been unselected). I do this in the AfterSelectChange event. This works fine if I select row 1 (and turn it green), then select row 2 (and turn it green) and then return to row 1 and select it again (which will turn it back to white to indicate unselection). This works because each click fires the AfterSelectChange event, so I can do what I want within that event. My problem is in a case where the users selects a row and wishes to immediately unselect it without going to another row in between. In this situation, the AfterSelectChange event does not fire again because no select "change" has taken place. I tried using the MouseDown event, but in that event I can't tell if the user clicked on a cell (which should not select/unselect the row) or on the RowSelector (which should select/unselect the row). Can you suggest any method I can use to get the functionality I want? Where and how can I ascertain that the user clicked on the RowSelector, even a dozen times in a row?
  • 469350
    Verified Answer
    Offline posted

    You can tell what part of the row the user clicked on inside the MouseDown event. You do this using UIElements. Although, personally, I would use MouseUp. 

    I'm not sure how this would work, though, since the grid is already going to try to select the row when you click on it and it is not already selected.So the row will always be selected in this event and there's no way to know if it was already selected when the user clicked on it. So I don't see any way to do what you are trying to do here unless you can somehow disable the grid's selection behavior while still allowing rows to be selected in code. 

    Just in case you want to experiment, here's how you determine if the user clicked on a row selector:

     

    private void ultraGrid1_MouseUp(object sender, MouseEventArgs e)
            {
                UltraGrid grid = sender as UltraGrid;

                // Get the last element entered by the mouse.
                UIElement element = grid.DisplayLayout.UIElement.LastElementEntered;

                // See if the element is a RowSelectorUIElement
                RowSelectorUIElement rowSelectorUIElement = element as RowSelectorUIElement;
                if (rowSelectorUIElement == null)
                {
                    // If the element is not a RowSelectorUIElement, see if it is the child of one.
                    rowSelectorUIElement = element.GetAncestor(typeof(RowSelectorUIElement)) as RowSelectorUIElement;
                }

                // If we still don't have a RowSelectorUIElement, then the user didn't click on
                // a row selector, so just exit.
                if (rowSelectorUIElement == null)
                    return;

                // Get the row
                UltraGridRow row = rowSelectorUIElement.GetContext(typeof(UltraGridRow)) as UltraGridRow;
                if (row == null)
                {
                    // Sanity check
                    Debug.Fail("We got a RowSelectorUIElement with no row context; unexpected.");
                    return;
                }
            }