# Monitor Selection in FlexGrid

## Content



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

### .NET Framework

Whenever the selection changes, either as a result of user actions or code, the grid fires the [SelectionChanged](/componentone/api/wpf/online-flexgrid/dotnet-framework-api/C1.WPF.FlexGrid.4.6.2/C1.WPF.FlexGrid.C1FlexGrid.SelectionChanged.html) 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:

```csharp
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](/componentone/api/wpf/online-flexgrid/dotnet-framework-api/C1.WPF.FlexGrid.4.6.2/C1.WPF.FlexGrid.CellRange.html) 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 **TopRow**, **BottomRow**, **LeftColumn**, and **RightColumn** properties instead of the **Row**, **Row2**, **Column**, 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.

```csharp
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.

### .NET

Whenever the selection changes, either as a result of user actions or code, the grid fires the [SelectionChanged](/componentone/api/wpf/online-flexgrid/dotnet-api/C1.WPF.Grid/C1.WPF.Grid.FlexGrid.SelectionChanged.html) 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:

```csharp
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](/componentone/api/wpf/online-flexgrid/dotnet-api/C1.WPF.Grid/C1.WPF.Grid.GridCellRange.html) 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 **TopRow**, **BottomRow**, **LeftColumn**, and **RightColumn** properties instead of the **Row**, **Row2**, **Column**, 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

[Select Cells and Objects](/componentone/docs/wpf/online-flexgrid/FlexGridFeatures/FlexGrid-Selection/Cell-Selection)