# DataView

The `IDataView` interface provides access to the view generated from the data displayed in the DataGrid instance.

The items in the `IDataView` can differ from the ones in the `ItemsSource` when a data operation, such as grouping, sorting, or filtering, is applied.

Before using the `IDataView`, check the `IDataView.IsDataReady` property or use the `DataBindingComplete` event or command.

The actual implementation of the `IDataView` is stateless and each `RadDataGrid.GetDataView` method call will return a new object instance.

## Properties

* `IsDataReady`—Determines whether all the internal data operations are completed and the view can be properly accessed.
* `Items`—Gets the top-level items within the view. The items can be either `IDataGroup` instances, or business objects if no grouping is applied.

## Methods

* `GetAggregateValue`(int `aggregateIndex`, `IDataGroup` group)—Gets the computed aggregate value associated with the provided aggregate index.
  * `aggregateIndex`—The zero-based index of the `AggregateDescriptorBase` within the `RadDataGrid.AggregateDescriptors` collection.
  * `group`—The `IDataGroup` instance to get the value for. Pass `null` to retrieve the computed grand total value.

* `GetAggregateValues`(string `propertyName`, `IDataGroup` group)—Gets the computed aggregate values for all the `PropertyAggregateDescriptor` instances associated with the provided property name.
  * `propertyName`—The name of the property to filter aggregates by.
  * `group`—The `IDataGroup` instance to get the value for. Pass `null` to retrieve the computed grand total value.

* `GetGroups`(`Predicate<IDataGroup> condition = null`)—Enumerates all the present `IDataGroup` instances by using depth-first approach. You can use an optional condition to filter the results.

* `GetIsExpanded`(`IDataGroup` group)—Determines whether the provided group is considered "expanded", that is, to have its `ChildItems` available within the UI.

* `GetParentGroup`(`object` item)—Gets the `IDataGroup` instance where the specified item resides. This method will return `null`, if no grouping is applied or the item does not belong to the `ItemsSource` of the grid.

* `ExpandGroup`(`IDataGroup` group)—Attempts to expand the provided `IDataGroup` instance.

#### **Use the ExpandGroup Method**

```C#
var currentView = this.dataGrid.GetDataView();

if (currentView.IsDataReady)
{
	currentView.ExpandGroup(currentView.Items[0] as IDataGroup);
}
```

* `CollapseGroup`(`IDataGroup` group)—Attempts to collapse the provided `IDataGroup` instance.

#### **Use the CollapseGroup Method**

```C#
var currentView = this.dataGrid.GetDataView();

if (currentView.IsDataReady)
{
	currentView.CollapseGroup(currentView.Items[0] as IDataGroup);
}
```

* `ExpandAll`()—Expands all the groups.

#### **Use the ExpandAll Method**

```C#
var currentView = this.dataGrid.GetDataView();

if (currentView.IsDataReady)
{
	currentView.ExpandAll();
}
```

* `CollapseAll`()—Collapses all the groups.

#### **Use the CollapseAll Method**

```C#
var currentView = this.dataGrid.GetDataView();

if (currentView.IsDataReady)
{
	currentView.CollapseAll();
}
```

* `ExpandItem`(`object` item)—Expands the chain of groups where the specified item resides.

#### **Use the ExpandItem Method**

```C#
var currentView = this.dataGrid.GetDataView();

if (currentView.IsDataReady)
{
	currentView.ExpandItem(this.dataGrid.SelectedItem);
}
```

* `CollapseItem`(`object` item)—Collapses the immediate groups that contain the specified item.

#### **Use the CollapseItem Method**

```C#
var currentView = this.dataGrid.GetDataView();

if (currentView.IsDataReady)
{
	currentView.CollapseItem(this.dataGrid.SelectedItem);
}
```

## Using the DataBindingComplete Command

The following examples respectively demonstrate how to create a command that collapses all groups except the first one each time when the DataGrid is grouped, and to add the custom command to the `Commands` of the DataGrid.

#### **Custom DataBindingComplete Command**

```C#
public class DataBindingComplete : DataGridCommand
{
	public override bool CanExecute(object parameter)
	{
		if ((parameter as DataBindingCompleteEventArgs).ChangeFlags.HasFlag(DataChangeFlags.Group))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public override void Execute(object parameter)
	{
		this.Owner.GetDataView().CollapseAll();

		if (this.Owner.GroupDescriptors.Count > 0)
		{
			this.Owner.GetDataView().ExpandGroup((this.Owner.GetDataView().Items[0] as IDataGroup));
		}

		base.Execute(parameter);
	}
}
```

#### **Add the Custom Command to the Commands Collection**

```XAML
<Grid xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid">
	<telerikGrid:RadDataGrid >
		<telerikGrid:RadDataGrid.Commands>
			<local:DataBindingComplete Id="DataBindingComplete" />
		</telerikGrid:RadDataGrid.Commands>
	</telerikGrid:RadDataGrid>
</Grid>
```

## See Also

* [Commands](https://www.telerik.com/winui/documentation/controls/raddatagrid/commands/datagrid-overview)
* [Aggregates](https://www.telerik.com/winui/documentation/controls/raddatagrid/aggregates/datagrid-overview)
