Search code examples
c#.netwinformsdevexpressxtragrid

List of unique values in a Column in XtraGrid


enter image description here

Is there a way to get the list of unique values in any Column in XtraGrid. When I click on the column title for filtering, it shows the list (see the attached image '}' ).

Is there any XtraGrid object or Column object by which I can get this in form of array or list ?


Solution

  • How about you implement it as an extension method on the GridView like this

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using DevExpress.XtraGrid.Views.Grid;
    
    namespace Extensions
    {
        public static class XtraGridExtensions
        {
            public static IEnumerable<string> GetColumnsDistinctDisplayText(this GridView gv, string columnName)
            {
                if (gv == null)
                {
                    throw new NullReferenceException("GridView is null");
                }
                if (gv.RowCount == 0)
                {
                    return Enumerable.Empty<string>();
                }
                return (from int row in Enumerable.Range(0, gv.RowCount - 1)
                        select gv.GetRowCellDisplayText(row, columnName)).Distinct().OrderBy(s => s);
            }
        }
    }
    

    and whenever you want to use it use it like this

    using Extensions;
    
    ...
    
    string msg = string.Empty;
    foreach (var item in gridView1.GetColumnsDistinctDisplayText("columnName"))
    {
        msg += item + "\n";
    }
    MessageBox.Show(msg);