Back to Devexpress

DataControlBase.CustomUniqueValues Event

wpf-devexpress-dot-xpf-dot-grid-dot-datacontrolbase-984ba6e4.md

latest8.8 KB
Original Source

DataControlBase.CustomUniqueValues Event

Allows you to specify unique values displayed within a column’s Drop-down Filter.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.Core.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event CustomUniqueValuesEventHandler CustomUniqueValues
vb
Public Event CustomUniqueValues As CustomUniqueValuesEventHandler

Event Data

The CustomUniqueValues event's data class is CustomUniqueValuesEventArgs. The following properties provide information specific to this event:

PropertyDescription
AsyncCompletedGets an AsyncCompletedHandler delegate referencing a method that should be executed after the operation is completed.
ColumnGets a column for which a drop-down filter is shown.
FilterGets the current filter.
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
IncludeFilteredOutGets whether a drop-down filter’s values are displayed with ignoring filters by other columns.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoundDateTimeGets or sets whether to ignore time values when you filter by DateTime values.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
SourceGets a control ( GridControl or TreeListControl ) that raised the event.
UniqueValuesGets or sets unique values.
UniqueValuesAndCountsGets or sets unique values and their counts.
UniqueValuesAndCountsAsyncGets or sets a task that allows you to asynchronously return unique values and their counts.
UniqueValuesAsyncGets or sets a task that allows you to asynchronously return unique values.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

The CustomUniqueValues event fires before a column’s drop-down filter is populated with unique column values. This event allows you to manually specify filter values.

xaml
<dxg:GridControl Name="gridControl" ItemsSource="{Binding Products}"
                 CustomUniqueValues="gridControl_CustomUniqueValues">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="ProductName" IsSmart="True"/>
        <dxg:GridColumn FieldName="CategoryName" IsSmart="True"/>
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView/>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
void gridControl_CustomUniqueValues(object sender, CustomUniqueValuesEventArgs e) {
    if (e.Column.FieldName == "CategoryName") {
        e.Handled = true;

        // Synchronously:
        // Specify the UniqueValues / UniqueValuesAndCounts property.

        e.UniqueValuesAndCounts = GetProductsQueryCore()
            .GroupBy(x => x.CategoryName)
            .Select(x => new ValueAndCount(x.Key, x.Count()))
            .ToArray();

        // Asynchronously:
        // Specify the UniqueValuesAsync / UniqueValuesAndCountsAsync property.

        e.UniqueValuesAndCountsAsync = Task.Run(() => {
            return GetProductsQueryCore()
                .GroupBy(x => x.CategoryName)
                .Select(x => new ValueAndCount(x.Key, x.Count()))
                .ToArray();
            });
    }
}
vb
Private Sub gridControl_CustomUniqueValues(ByVal sender As Object, ByVal e As CustomUniqueValuesEventArgs)
    If e.Column.FieldName = "CategoryName" Then
        e.Handled = True
        e.UniqueValuesAndCounts = GetProductsQueryCore()
            .GroupBy(Function(x) x.CategoryName)
            .[Select](Function(x) New ValueAndCount(x.Key, x.Count()))
            .ToArray()

        e.UniqueValuesAndCountsAsync = Task.Run(Function() 
            GetProductsQueryCore()
                .GroupBy(Function(x) x.CategoryName)
                .[Select](Function(x) New ValueAndCount(x.Key, x.Count()))
                .ToArray())
    End If
End Sub

If your data source implements the IQueryable interface, you can use the GridQueryableExtensions.Distinct and GridQueryableExtensions.DistinctWithCounts methods from the DevExpress.Xpf.Grid.25.2.Extensions.dll library to obtain unique values.

csharp
void gridControl_CustomUniqueValues(object sender, CustomUniqueValuesEventArgs e) {
    if (e.Column.FieldName == "CategoryName") {
        e.Handled = true;
        e.UniqueValuesAndCountsAsync = Task.Run(() => {
            return GetProductsQueryCore().DistinctWithCounts(e.Column.FieldName);
        }
    }
}
vb
Private Sub gridControl_CustomUniqueValues(ByVal sender As Object, ByVal e As CustomUniqueValuesEventArgs)
    If e.Column.FieldName = "CategoryName" Then
        e.Handled = True
        e.UniqueValuesAndCountsAsync = Task.Run(Function() 
            GetProductsQueryCore().DistinctWithCounts(e.Column.FieldName))
    End If
End Sub

If you want to maintain a clean MVVM pattern and specify a custom set of unique values in a View Model, create a command and bind it to the CustomUniqueValuesCommand property.

See Also

DataControlBase Class

DataControlBase Members

DevExpress.Xpf.Grid Namespace