This is a migrated thread and some comments may be shown as answers.

How to determine how many distinct values are displayed in a column

3 Answers 557 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Calvin
Top achievements
Rank 2
Calvin asked on 26 Jul 2011, 07:24 PM
I would like to determine how many distinct values are shown in a grid column after filtering is applied.

The filter parameter (true) in grid.GetDistinctValues(column, true, null) considers the other columns (but not the specified column).  So this method returns every distinct value for the considered column and is not restricted to just those distinct values that are actually shown in the column as a result of the filtering currently applied to that column as well as to the other columns.

So this method doesn't suit my purpose.

I would like to know the number of distinct values actually shown in a column to support the following two scenarios.  (1) If there are zero distinct values displayed in a column, then the last filtering operation over-constrained the result set and can be undone (with user notification).  (2) If there is only one distinct value, then the column can be hidden and the column-header/single-unique-value pair can be transferred to a "common values" grid.

This approach works well with our datasets.  As the user applies filtering operations, an 8000 row, 65 column datasource typically gets reduced to about 20 rows and 5 to 10 columns each containing more than one distinct value.  All other single-valued columns are displayed in an adjacent common-values grid.

In the grid.FilterDescriptors.CollectionChanged event, I invoke the method which consolidates the common-values.  For this method, I need to know if there is only one distinct value.

Many thanks!

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 27 Jul 2011, 08:58 AM
Hi Calvin,

Why don't you use the Distinct method over RadGridView.Items. RadGridView.Items are the items that are currently in the view. So if the grid is bound to a collection of Persons then you could do something like this:

var distinctgaes = this.radGridView.Items.Cast<Person>().Select(person=>person.Age).Distinct();

This should return all the distinct ages that are currently in the view. In fact, we use the Distinct method internally as well.

Let me know if that approach is not feasible or does not work.

Best wishes,
Ross
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Calvin
Top achievements
Rank 2
answered on 28 Jul 2011, 06:45 PM
Thank you, the proposed technique suits my purposes.  But it doesn't seem to work.

I.e,. in the radGridView.FilterDescriptors.CollectionChanged event, the radGridView.Items.Cast<Person>().Select(person=>person.Age) statement is returning all (bound) items rather than just the filtered items.

Secondly, there is another issue with this approach.  I need to dynamically project a property based on its name and type.  I.e., I have for inputs the bound column's DataMemberName() and DataType.

So I want to replace:

  radGridView.Items.Cast<Person>().Select(person=>person.Age).Distinct();

with:

  radGridView.Items.Cast<Person>().Select( Projector<Person, typeof(Age)>("Age") ).Distinct();

But since a generic function requires that its type-arguments' types be specified at compile time, the foregoing signature won't work.

Taking a simpler function Projector<Person, int>("Age"), how does one code this method?

One could always generalize this method using an object type parameter and subsequently cast it.  But that would be inelegant.


Update
I got around the first issue by moving the code from the radGridView.FilterDescriptors.CollectionChanged event to the radGridView.Items.CollectionChanged event.  Calling radGridView.Items.Refresh() in the FilterDescriptors.CollectionChanged event did not update the Items collection to reflect the latest filtering operation.

I got around the second issue via
   radGridView.Items.Cast<Person>().Select(p => p.GetType().GetProperty(columnName).GetValue(p, null)).Distinct()

The deficiency here is that it returns an IEnumerable<object> rather than an IEnumerable<column's-type>.  But it works for the task at hand.
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 07 Mar 2018, 06:29 AM

 

Hi,

I have a Radgrid with values like below Pic(A),

and I want get two results without loop

  1. Item_Name and Count of Item_Name, like below pic (B)
  2. Item_Code  and Count of Item_Code , like below pic (C)

I can get it with Linq from datatable, but I want directly from Radgrid.

How could I do this, please refer

Thank you,

Jamsheer

 

 

Tags
GridView
Asked by
Calvin
Top achievements
Rank 2
Answers by
Rossen Hristov
Telerik team
Calvin
Top achievements
Rank 2
jamsheer
Top achievements
Rank 1
Veteran
Share this question
or