I have a grid with the GroupBy feature turned on.
I'd like to add some space between the groups that look like this...
However, I always seem to end up with some space between the header and the first group
Or I end up with space between the groups and the rows in that group.
I was trying to use...
GroupByRowSpacingBefore and GroupByRowSpacingAfter
Are there other settings I should consider trying?
Thanks,
Brian.
Hi Brian,
Hm, this will be a bit tricky. If you use GroupByRowSpacingBefore, then there will be a space before each group, which means a space between the headers and the first visible GroupByRow.
GroupByRowSpacingAfter won't work, because that will create space between the GroupByRow and it's child rows.
It seems like what you really want is a space after the last data row in each group. Or a space before each GroupByRow except the first one.
It looks like you must be setting the HeaderPlacement to get the headers to appear only once at the top, too. Or possibly you are setting ViewStyle to SingleBand, which would have the same effect. If you don't do this, then the headers will appear under each GroupByRow and then using GroupByRowSpacingBefore would not be a problem.
So in order to make this work, you would have to dynamically change the spacing before the row based on whether the GroupByRow is the first visible row or not. The obvious way to acheive this would be to handle the Before or AfterRowRegionScroll event and set the RowSpacingBefore on each GroupByRow. But this is a bad idea, because you will essentially be changing the scroll metrics during a scroll operation and that's going to cause problems.
I was able to get this to work like this:
UltraGridGroupByRow lastAdjustGroupbyRow = null; private void ultraGrid1_AfterRowRegionScroll(object sender, RowScrollRegionEventArgs e) { this.AdjustGroupByRowSpacing(e.RowScrollRegion); } private void AdjustGroupByRowSpacing(RowScrollRegion rowScrollRegion) { this.ultraGrid1.BeginUpdate(); try { if (lastAdjustGroupbyRow != null) lastAdjustGroupbyRow.RowSpacingBefore = -1; foreach (VisibleRow visibleRow in rowScrollRegion.VisibleRows) { UltraGridGroupByRow groupByRow = visibleRow.Row as UltraGridGroupByRow; if (groupByRow != null) { if (rowScrollRegion.VisibleRows.IndexOf(visibleRow) == 0) { groupByRow.RowSpacingBefore = 0; this.lastAdjustGroupbyRow = groupByRow; } } } } finally { this.ultraGrid1.EndUpdate(); } }
UltraGridGroupByRow lastAdjustGroupbyRow = null; private void ultraGrid1_AfterRowRegionScroll(object sender, RowScrollRegionEventArgs e) { this.AdjustGroupByRowSpacing(e.RowScrollRegion); }
private void AdjustGroupByRowSpacing(RowScrollRegion rowScrollRegion) { this.ultraGrid1.BeginUpdate(); try { if (lastAdjustGroupbyRow != null) lastAdjustGroupbyRow.RowSpacingBefore = -1; foreach (VisibleRow visibleRow in rowScrollRegion.VisibleRows) { UltraGridGroupByRow groupByRow = visibleRow.Row as UltraGridGroupByRow; if (groupByRow != null) { if (rowScrollRegion.VisibleRows.IndexOf(visibleRow) == 0) { groupByRow.RowSpacingBefore = 0; this.lastAdjustGroupbyRow = groupByRow; } } } } finally { this.ultraGrid1.EndUpdate(); } }
But this code is not great, because there's a nasty flicker every time you scroll the grid vertically. Also, you have to call something like this in the Form_Load or at some point after the grid is bound:
this.AdjustGroupByRowSpacing(this.ultraGrid1.DisplayLayout.RowScrollRegions[0]);
Anyway, like I said, this is probably not a good idea since it changes the scroll metrics while in the middle of a scroll operation. There may be other side effects in addition to the flicker. So if the spacing is that important to you, I'd recommend putting the headers under each group.
Hi Mike,
Thanks for the suggestions.
However, I was able to get the behavior I wanted by just locating the last row in each group and adding some space after that row.
When the user resorts the rows (during AfterSortChange(..) event) I remove the spacing on the last row and then find the new last row and add some extra spacing after that row.
So, far this seems to work very well.
Here is how I located the last row in each group...
//Keep some space after the last row in each group
foreach(UltraGridRow possibleGroupedRowsCollection in ugProjects.Rows)
{
if (possibleGroupedRowsCollection.IsGroupByRow == true)
UltraGridGroupByRow groupedRowsCollection = (UltraGridGroupByRow)possibleGroupedRowsCollection;
int lastRowIndex = groupedRowsCollection.Rows.Count - 1;
foreach (UltraGridRow row in groupedRowsCollection.Rows)
//Reset all of the other rows.
row.RowSpacingAfter = 0;
}
//Add some row spacing after the last row in the group
groupedRowsCollection.Rows[lastRowIndex].RowSpacingAfter = 10;