Question

How to get distinct values of a Data Grid?

Hi,

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.

Thanks

**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.

Comments

Keep up to date on this post and subscribe to comments

Pega
August 1, 2017 - 7:44am

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

http://help.openspan.com/80/Components/Data_Table_Viewer.htm

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

Hi,

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.

Thanks

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"),                           
                     })
                    .Distinct().ToList();

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.
        MessageBox.Show(p.GetValue(o).ToString());
    }

}