My grid has the following columns:
Codenumber | Price | Component | Qty
123 10 ABC 1
123 10 DEF 2
123 10 GHI 3
456 50 JKL 4
456 50 MNO 5
I need to group by codenumber and price, but they should both be on the same line. It shouldn't go from Codenumber down to price and then down to component and qty. Codenumber and price should be 1 group.
I also need to do this in code since I am supplying the datasource at runtime and can have different columns. Depending on the columns I may not want to group it this way.
Is there some kind of property to set this or code that can group it this way?
I appreciate any help. Thank you.
The easiest way to handle this would be to use an unbound column. What you would do is use the InitializeLayout event of the grid to add a column to the root band. You can make the DataType of this column anything you want, but I would probably use a string, and then you can populate the column with values containing what you want displayed in the GroupByRow.
Then you group the grid by the unbound column (also in InitializeLayout).
Then you would use the InitializeRow event to populate the unbound column with the combined values of the Codenumber and Price columns.
Hi Mike,I currently read this article because I need something similar like bdisk. This is few years old now so maybe there is another solution.I am searching for the following:I have e.g. 6 rows in my grid with the following properties:1 age 20, job developer, country UK2 age 20, job developer, country UK3 age 25, job doctor, country USA4 age 25, job doctor, country USA5 age 30, job professor, country DE6 age 32, job worker, country DEThe only thing which I found is that the grid does the group by only on just one property. So first the age is grouped by, then the job and then the country.So in result I have three bands. But I now want a general group by on all the three parameters like it would be in a SQL statement on database.So row 1 and 2 should be grouped by because all values are the same. Same thing for row 3 and 4. Row 5 and 6 are the only rows with its combination so they are "alone".So after grouping by I want as result 4 rows in my grid. When I expand the first group by row, the row 1 and 2 should be visible as child bands, when I expand second group by row, row 3 and 4 are visible as child rows and so on...Is this behavior possible in any way?
Thanks in advance,
Alex
InitializeRow shouldn't take very much time, even with a lot of rows. The only way it would cause a problem is if the code you have in the event handler is not efficient. Check out the WinGrid Performance Guide for some tips on writing the most efficient code inside that event.
And alternative solution would be to use a GroupByComparer. But I don't think the GroupByComparer is any more efficient. You still have to compare the values of all 3 fields.
Hi Mike,
its working for me yes, I was just asking for another solution because initialize Row event needs time because I have lots of jobs in my dataset. But your solution is basically working for me.
Thanks,
Hi Alex,
If I understand your requirement correctly, then it seems as if the answer I posted here (using an unbound column) should work for you. Are you having trouble getting this to work? Is there some reason that the approach I posted above will not work for this case?
Another option would be to use a GroupByComparer. But it's essentially the same thing. The comparer evaluates each row and some up with some kind of combined value and the compares based on the combination.
The advantage of the unbound column is that it's a bit simpler to implement.