FlexGrid for WPF | ComponentOne
Features / Selection / Monitor Selection in FlexGrid
In This Topic
    Monitor Selection in FlexGrid
    In This Topic

     In the the following section learn how to implement Monitor Selection in FlexGrid .NET 4.5.2 and .NET 5 versions.

    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 sends information to the console when the selection changes:

    C#
    Copy Code
     private void grid_SelectionChanged(object sender, CellRangeEventArgs e)
            {
                CellRange sel = grid.Selection;
                Console.WriteLine("selection: {0}, {1} - {2}, {3}",
                    sel.Row, sel.Column, sel.Row2, sel.Column2);
                Console.WriteLine("selection content: {0}",
                GetClipString(grid, sel));
            }
    
            static string GetClipString(C1FlexGrid grid, 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", grid[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 performs the selection (dragging the mouse up or down while selecting).

    You can easily extract a lot of useful information from the Selection using the GetDataItems method, which returns a collection of data items associated with a cell range. 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 grid_SelectionChanged(object sender, CellRangeEventArgs e)
    {
      // get customers in the selected range
      var customers =
        grid.Rows.GetDataItems(grid.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());
    }
    

    The above 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.

    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 sends information to the console when the selection changes:

    C#
    Copy Code
    private void grid_SelectionChanged(object sender, C1.WPF.Grid.GridCellRangeEventArgs e)
    {
        //DOC Code Snippet1
        GridCellRange sel = grid.Selection;
        Console.WriteLine("selection: {0}, {1} - {2}, {3}", sel.Row, sel.Column, sel.Row2, sel.Column2);
        Console.WriteLine("selection content: {0}",
        GetClipString(grid, sel));
    }
    
    static string GetClipString(FlexGrid grid, GridCellRange sel)
    {
        var sb = new System.Text.StringBuilder();
        for (int r = sel.Row; r <= sel.Row2; r++)
        {
            for (int c = sel.Column; c <= sel.Column2; c++)
            {
                sb.AppendFormat("{0}\t", grid[r, c].ToString());
            }
            sb.AppendLine();
        }
        return sb.ToString();
    

    Whenever the selection changes, the code lists the coordinates of the GridCellRange 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 GridCellRange'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 performs the selection (dragging the mouse up or down while selecting).

    See Also