Hi everyone,
I put an UltraWinGrid into an other UltraWinGrid using an UltraControlContainerEditor.
Now I would like the Cell size to fit the contained grid size. In order to do it, I would like to get the real size of the grid and not the size of the component.
Thanks
Hi,
The way the sizing works is that the size of the grid cell is determined by the editor. When you auto-size a column in the grid, the grid goes through each row and asks the editor for the ideal size it wants to be. So in the case of a normal text cell, the EditorWithText measures the current text in the cell using the current font settings and adds some padding for the left and right edges and returns this to the grid.
The ControlContainerEditor obviously doesn't know the ideal size of it's contents, since it could contain any control. So what it does is it uses the PreferredSize on the control that you speficied. If preferredSize is not set, it will fall back to the actual Size of the control.
So in order to get what you want here, you would have to derive a control from the grid and override the GetPrefferredSize method to return the size you want.
Now... in your case, you want to get the size of the grid so that all of the columns and rows are displayed with no scrollbars. Unfortunately, the grid does not have any functionality for this. There's nothing built-in to the grid that calculates an ideal size. In fact, this kind of calculations is quite incredibly complex for the grid to do and account for all possible options, since the grid has so many options and features.
You can make a pretty good guess at the size by summing up the widths of the columns and adding in the width of the vertical scrollbar (if it's visible). You would also have to account for RowSelectors and indentation of the bands (if your grid is hierarchical, which it probably isn't in this case) and the grid's own borders. For the height, you have to account for headers, rows, the horizontal scrollbar, and the grid borders.
You may also need to fudge the numbers a little bit, because cells in the grid sometimes overlap by a pixel.
"Now... in your case, you want to get the size of the grid so that all of the columns and rows are displayed with no scrollbars. Unfortunately, the grid does not have any functionality for this. There's nothing built-in to the grid that calculates an ideal size. In fact, this kind of calculations is quite incredibly complex for the grid to do and account for all possible options, since the grid has so many options and features."
Mike, there must be a way to do this. The grid somehow has to determine when to show scrollbars and when not to. What is that code doing?
I certainly agree with your assessment that the calculation would be complex. I've been messing around for hours trying to do exactly what the original poster wants to do. I'm only working on the vertical dimension. I've tried adding up all the row heights and column header heights, accounted for borders, and even putting in a fudge factor for the cell overlap by 1 pixel that you mentioned. I get it working for a grid with about 5 rows, but it doesn't work for a grid with 10 rows. Then I get it working for 10 rows, but it stops working for 5 rows...very frustrating.
bralston7 said:Mike, there must be a way to do this. The grid somehow has to determine when to show scrollbars and when not to. What is that code doing?
For the columns, you may be right. There are methods on the band call GetExtent. In a single-band flat grid, you could use this method to determine the width that the band needs and then you might just have to account for ScrollBars and borders. So it might be possible to get the correct width that the grid should be.
For the height, it's much more complex. Vertical scrolling in the grid in row-based, not pixel-based. So it's the grid just counts the number of rows and determines which ones are in the visible area. It never has to calculate the total height needed for all the rows - it just counts the number of rows and then displays some rows using UIElements and if it runs out or room, it knows it needs a scrollbar.
This is a vast oversimplification, of course.