ComponentOne FlexGrid for UWP
Features / Selection Modes / Monitoring the Selection
In This Topic
    Monitoring the Selection
    In This Topic

    Whenever the selection changes, either as a result of user actions or code, the grid fires the SelectionChanged event, which allows you to react to the new selection.

    For example, the code below monitors the selection and outputs information to the console when the selection changes:

    C#
    Copy Code
    void _flex_SelectionChanged(object sender, CellRangeEventArgs e)
    {
      CellRange sel = _flex.Selection;
      Console.WriteLine("selection: {0},{1} - {2},{3}",
          sel.Row, sel.Column, sel.Row2, sel.Column2);
      Console.WriteLine("selection content: {0}",
          GetClipString(_flex, sel));
    }
    static string GetClipString(C1FlexGrid fg, CellRange sel)
    {
      var sb = new System.Text.StringBuilder();
      for (int r = sel.TopRow; r <= sel.BottomRow; r++)
      {
        for (int c = sel.LeftColumn; c <= sel.RightColumn; c++)
        {
          sb.AppendFormat("{0}\t", fg[r, c].ToString());
        }
         sb.AppendLine();
      }
      return sb.ToString();
    }
    

    Whenever the selection changes, the code lists the coordinates of the CellRange that represents the current selection. It also outputs the content of the selected range using a GetClipString method that loops through the selected items and retrieves the content of each cell in the selection using the grid's indexer described earlier in this document.

    Notice that the for loops in the GetClipString method use the CellRange's TopRowBottomRowLeftColumn, and RightColumn properties instead of the RowRow2Column, and Column2 properties. This is necessary because Row may be greater or smaller than Row2, depending on how the user performed the selection (dragging the mouse up or down while selecting).

    You can easily extract a lot of useful information from the Selection using the RowCollection.GetDataItems method. This method returns a collection of data items associated with a CellRange. Once you have this collection, you can use LINQ to extract and summarize information about the selected items.

    For example, consider this alternate implementation of the SelectionChanged event for a grid bound to a collection of Customer objects:

    C#
    Copy Code
    void _flex_SelectionChanged(object sender, CellRangeEventArgs e)
    {
      // get customers in the selected range
      var customers =
        _flex.Rows.GetDataItems(_flex.Selection).OfType<Customer>();
      // use LINQ to extract information from the selected customers
      _lblSelState.Text = string.Format(
        "{0} items selected, {1} active, total weight: {2:n2}",
        customers.Count(),
        (from c in customers where c.Active select c).Count(),
        (from c in customers select c.Weight).Sum());
    }
    

    Notice how the code uses the OfType operator to cast the selected data items to type Customer. Once that is done, the code uses LINQ to get a total count, a count of "active" customers, and the total weight of the customers in the selection. LINQ is the perfect tool for this type of job. It is flexible, expressive, compact, and efficient.