Search code examples
c#groupingobjectlistview

Grouping by non-column in objectlistview


I have about 5-6 rows of data that I need to display, when I add data I'd like to be able to sort the data in 4 different sections and none of these will be grouped by any column value its simply getting grouped by a SQL value etc.

How do you do this effectively with the objectlistview?

Its pretty straightforward with the standard listview:

listView1.Items[0].Group = listView1.Groups[0];

I can't seem to figure it out in the objectlistview though.

Thanks.

Adding code:

    public class AdminHistory
    {
        public string Ordernr { get; set; }
        public string newImei { get; set; }
        public string Imei { get; set; }
        public string Fel { get; set; }
        public string Modell { get; set; }
        public string Garanti { get; set; }
        public string Datum { get; set; }
        public string User { get; set; }
        public string Status { get; set; }

    }

    private void admin_Load(object sender, EventArgs e)
    {
        List<AdminHistory> list = new List<AdminHistory>();

        list.Add(new AdminHistory() { Ordernr = "12345", newImei = "", 
            Imei = "test", Fel = "test", Modell = "abc", 
            Garanti = "ja", Datum = "2014-01-01 18:31", User = "123", Status = "waiting"});

        adminHistory.SetObjects(list);
    }

My objectlistview has the propper aspectnames.

Basically I don't want to group by any of the fields above, I simply want to be able to create a virtual group and group a lot of data into 4 separate groups, then these groups will be displayed in the listview and the user may view them as he/she wishes.

Example image by creators of objectlistview:
http://www.codeproject.com/KB/list/ObjectListView/fancy-screenshot2.png

The grouping in that image does not group by a column, (I think atleast) but rather a virtual group just like I'm trying to achive.


Solution

  • The grouping in that image does not group by a column, (I think atleast) but rather a virtual group just like I'm trying to achive.

    The example actually IS grouped by a columns aspect value. But it additionally uses the "Grouping by Ratings" feature using groupColumn.MakeGroupies.

    As far as I know, grouping is always bound to a column. However, you could achieve what you want with a small workaround.

    "The crucial part to understand is that all model objects that have the same “key” are placed in the same group. By default, the “key” is the aspect of the model object as calculated by the grouping column."

    You can return your own "group key" by installing a GroupKeyGetter delegate, but this property is bound to a column. However, you could just use a "spare" column of your choice , install the GroupKeyGetter for it and return the desired "key" which could be anything.

    mySortColumnOfChoice.GroupKeyGetter = delegate(object rowObject) {
        var key = /* create your key here */
        return key;                             
    };
    

    Then use

    objectListView1.BuildGroups(mySortColumnOfChoice, SortOrder.None);
    

    to create the group using your "custom" key.