.NET, Code, Infragistics, WinGrid

Infragistics WinGrid – Tips and Tricks #1 – Right Click Row Select

I’ve been working with Infragistics tools, specifically the UltraWinGrid for 10 years or more (worked with the Sheridan grid before that, anyone remember those tools?), and outside of the Infragistics forums there seems to be very little in the way of information on this very powerful and complex tool.  Perhaps I can leverage my experiences with these tools into a nice little series of posts on things I’ve discovered over the years.

I came upon a situation where I wanted to right click on a grid, have it select the row under the mouse and bring up a context menu to act on that selected row.  As another requirement I wanted it to only act upon data rows.

I came up with the following extension method.

  1. First get the element under the mouse and determine if it’s a row element.
  2. If it’s a row, is it a datarow?
  3. Make it the active row.
  4. if the row is not already selected, select it and deselect any other row.
  5. if the row is already selected, then do nothing else special with the selection properties.
  6. if we’ve clicked somewhere not a datarow, I clear any selected rows in the grid.
21 public static void RightClickRowSelect(thisUltraGrid grid, Point mouseLocation)  
22 {  
23     UIElement element = grid.DisplayLayout.UIElement.ElementFromPoint(mouseLocation);  
24     var row = element.GetContext(typeof(UltraGridRow)) asUltraGridRow;  
25     if (row != null && row.IsDataRow)  
26     {  
27         grid.ActiveRow = row;  
28         if (!row.Selected)  
29         {  
30            grid.Selected.Rows.Clear();  
31            row.Selected = true;  
32         }  
33     }  
34     else  
35     {   
36        grid.Selected.Rows.Clear();  
37     }  
38  }

Then to use this, I simply call it in the MouseDown event on the grid as follows.

    1     privatevoid grdLabsMouseDown(object sender, MouseEventArgs e)
    2     {
    3       if (e.Button == MouseButtons.Right)
    4       {
    5         grdLabs.RightClickRowSelect(e.Location);
    6       }
    7    }

Pretty basic really, but knowing how to determine what element is under the mouse is a critical tool in your belt when dealing with the WinGrid and opens up the door for a lot of advanced features.