How to get distinct values of a Data Grid?


I would like to get the distinct column from a Data Grid. Example, data grid has the following records:

ID | Name | Country


1 | John | US

2 | Fred | Canada

3 | Ben | US

Would like to get the distinct country (US and Canada) and display it on another DataGrid or to Data Table.


**Moderation Team has archived post**

This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.


Keep up to date on this post and subscribe to comments

August 1, 2017 - 7:44am

The TableView component has a method ToTable which will do this.

August 1, 2017 - 10:10am
Response to jeffbadger


May I know how to populate TableView from a DataGrid source? Thanks

August 2, 2017 - 8:46am

Hi, can anyone provide detailed step on this? I'm new to DataGrid/DataTable/TableView.


August 28, 2017 - 11:40am

Hi ,

    You can use the following code to get the distinct values. Hope this helps.

var distinctIds = dt.AsEnumerable()
                    .Select(s=> new {
                        id = s.Field<string>("YourColumnName"),                           

foreach(object o in distinctIds)
    Type t = o.GetType();

    PropertyInfo[] pi = t.GetProperties();

    foreach (PropertyInfo p in pi)
        //Here you will get distinct value for your given column.