Read More on Telerik Blogs
January 07, 2010 Web
Get A Free Trial

With our upcoming service pack (Q3 2009 SP2) you will be able to populate distinct values asynchronously using the DistinctValuesLoading event. To achieve this you can assign any INotifyCollectionChanged collection for distinct values ItemsSource and RadGridView will update the UI immediately on any change.

I’ve made small demo on how to achieve this using WCF RIA Services and RadGridView bound to DomainDataSource with DataPager – in this case the grid distinct values are populated from the entire collection server-side not only from the current downloaded page:
 

server-side:

public List<DistinctValue> GetDistinctValues(string propertyName)
{
    var list = new List<DistinctValue>();
    var values = this.DataContext.Customers.Select(propertyName).OfType<string>().Distinct().ToList();

    for (var i = 0; i < values.Count; i++)
    {
        list.Add(new DistinctValue() { ID = i, Value = values[i] });
    }

    return list;
}
...
public class DistinctValue
{
    [Key]
    public int ID { get; set; }
    public string Value { get; set; }
}
...

client-side:

private void RadGridView1_DistinctValuesLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDistinctValuesLoadingEventArgs e)
{
    var collection = new RadObservableCollection<string>();
    e.ItemsSource = collection;

    var context = (NorthwindDomainContext)DomainDataSource1.DomainContext;
    context.DistinctValues.Clear();
    context.Load(context.GetDistinctValuesQuery(e.Column.UniqueName)).Completed += (s, args) => 
    {
        collection.AddRange(context.DistinctValues.OfType<DistinctValue>().Select(dv => dv.Value));
    };
}


About the Author

Vladimir Enchev

is Director of Engineering, Native Mobile UI & Frameworks