How to make a Grid auto-expand its columns to fit the grid's size?

I have a Grid and I’d like the columns to expand to fill all the available width of the grid if they have extra space available. I have a Panel whose size gets set to the desired width, and I currently have the following code to deal with the grid:

self.grid.AutoSize()

gridW, gridH = self.grid.Size

W, H = self.Size

self.grid.SetSize((W, gridH))

This does set the grid’s size to fit the given width. However, the columns don’t expand, the grid just draws some extra white & gray space: http://www1.picturepush.com/photo/a/12463819/img/12463819.png . How do I make the columns expand? I tried setting their sizes manually to fill the available width but then I get scrollbars appearing & messing everything up. Until then, I’ve settled on the following:

self.grid.AutoSize()

gridW, gridH = self.grid.Size

W, H = self.Size

self.grid.SetPosition(((W - gridW) / 2, 0))

Result: http://www4.picturepush.com/photo/a/12463832/img/12463832.png . But that’s far from ideal…

Thanks,

  • Claudiu

Claudiu Saftoiu wrote:

I have a Grid and I'd like the columns to expand to fill all the
available width of the grid if they have extra space available. I have a
Panel whose size gets set to the desired width, and I currently have the
following code to deal with the grid:

         self.grid.AutoSize()
         gridW, gridH = self.grid.Size
         W, H = self.Size
         self.grid.SetSize((W, gridH))

This does set the grid's size to fit the given width. However, the
columns don't expand, the grid just draws some extra white & gray space:
http://www1.picturepush.com/photo/a/12463819/img/12463819.png . How do I
make the columns expand? I tried setting their sizes manually to fill
the available width but then I get scrollbars appearing & messing
everything up.

You can factor the scrollbar width into your calculations by using wx.SystemSettings.GetMetric.

But even with that you may not be able to get an exact fit. Since wxGrid derives from wxScrolledWindow then it rounds up the virtual size to the next multiple of the scroll rate. I think that the grid uses a scroll rate (how many pixels are sifted when a scrollbar arrow is clicked) of 15, so that means that the virtual size of the grid could be up to 14 pixels larger than it needs to be, and those extra pixels will be visible when they are scrolled in to view. The scrollbars are hidden if the client size is larger than the virtual size.

···

--
Robin Dunn
Software Craftsman