views:

43

answers:

0

I have looked at Matt Berseth's Grouping Grid sample and have tried to adapt this to my needs, however my problem is a bit more sticky.

I have two tables, call them LineItems and Headers. I need to Select all items from LineItems that meet a specific criteria, and group the items under various headers that are somewhat fuzzy.

By fuzzy, I mean there is no direct correlation between them. Headers has a number of records that merely store an ID and some text to display as a header (no relation to any columns, it spans the width of the grid). The ID represents where in the grid the header should be inserted.

For example, suppose I have the following LineItems:

1,Description
2,Description
3,Description
4,Description
5,Description
6,Description
7,Description

Then I have the following Headers:

0,Text
5,Text
10,Text

This should be formatted in the table as:

0,Text
1,Description
2,Description
3,Description
4,Description
5,Text
5,Description
6,Description
7,Description
10,Text

In other words, Header must come before the equivelent LineItem of the same or larger number. This one has me scratching my head.

I'm using a ListView because I need the formatting capabilities of it. And i've tried several approaches, such as nested ListView's with the Header at the top level and LineItems in the nested. The problem is that since the key value is not an equals situation, and there is no good way to retain the state of the previous Header to do a between where clause, i'm not really sure how to approach this.

If I just say Where HeaderKey > LineItemKey then i'll get duplicate items in the groups. And I can't say Where HeaderKey > LineItemKey && LineItemKey < NextHeaderKey because I don't know what the next HeaderKey is.

For what it's worth, i'm using Linq-to-Sql for the queries. I would post some actual code, but I can't even get close to something that works, so i'm really just starting from scratch here. And no, I can't change the schema of the database.

UPDATE:

I'm trying to formulate a query with Linqpad that looks something like this:

from h in Headers where h.Category==0 select new {
    HeaderID = h.ID,
    Text = h.Text,
    LineItems = from li in LINEITEMs where li.ID > h.ID && li.ID < h.NextID select li }

This of course doesn't work, because there's no way to know what the next headerID is.