Show/Hide ListCtrl Column(s) in report mode

1,128 views
Skip to first unread message

dtp lists

unread,
Jul 13, 2011, 10:42:46 AM7/13/11
to wxpytho...@googlegroups.com
Hello all,

    Like many people before me, I have a wx.ListCtrl and would like to give the user the ability to show/hide columns within it. What I have currently is a ListCtrl and have bound an event handler to wx.EVT_LIST_COL_RIGHT_CLICK so that when the user right-clicks on a column they are presented with a wxMenu containing a check item for each column header. Some googling shows that a common approach to hide a column is to set the width to 0, however, this comes with a two specific issues:
        1. The user can un-hide the column by resizing the 0 width column
        2. Doing #1 mangles the state of the wxMenu check items.

Another approach I tested was to delete via ListCtrl.DeleteColumn() and re-insert the column via ListCtrl.InsertColumn() or ListCtrl.InsertItem(). Neither of these worked as expected because the underlying data was removed when the column was deleted.

So, does anybody have a suggestion as to how to accomplish this in an elegant way? My feeling is that there is no way to get around having to re-load the data every time a column is added or removed.

Thank you,

-Dave


Robin Dunn

unread,
Jul 13, 2011, 1:57:31 PM7/13/11
to wxpytho...@googlegroups.com

If you use the wx.LC_VIRTUAL style then the listctrl will ask you (via
overridden callback methods) for the values as they are needed for
display. IOW you do not load all the values into the listctrl when you
create it, so if you delete a column and add it again later you still
have the data available to display in that column. You'll just want to
come up with a good way to map your data values to the rows/cols of the
listctrl that takes into account that some data cols may not have active
listctrl cols.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Andrea Gavana

unread,
Jul 14, 2011, 5:16:04 AM7/14/11
to wxpytho...@googlegroups.com

Another possibility would be to use UltimateListCtrl in wx.lib.agw,
which allows hiding/showing columns by calling
SetColumnShown(column_number, show=True). More docs:

http://xoomer.virgilio.it/infinity77/AGW_Docs/ultimatelistctrl.UltimateListCtrl.html

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

dtp lists

unread,
Jul 14, 2011, 5:00:49 PM7/14/11
to wxpytho...@googlegroups.com
Andrea,

    The UltimateListCtrl is exactly what I was looking for! Thank you so much. One quick question regarding the column header height... is that adjustable somehow? The native wx.ListCtrl has a very thin header while the ultimatelistctrl has a slightly thicker one. There does not seem to be a straightforward way to resize the header hight via the API.

Thanks again,

-Dave


Andrea Gavana

unread,
Jul 21, 2011, 5:33:57 PM7/21/11
to wxpytho...@googlegroups.com
On 15 July 2011 00:00, dtp lists wrote:
> Andrea,
>
>     The UltimateListCtrl is exactly what I was looking for! Thank you so
> much. One quick question regarding the column header height... is that
> adjustable somehow? The native wx.ListCtrl has a very thin header while the
> ultimatelistctrl has a slightly thicker one. There does not seem to be a
> straightforward way to resize the header hight via the API.

There isn't actually a way to do it, the header height is pulled out
using wx.RendererNative:

wx.RendererNative.Get().GetHeaderButtonHeight(listctrl)

But it should be easy enough to add. If you can enter a bug report on
wxTrac that would be most helpful, so that I don't lose track of this
improvement (component=AGW).

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

==> Never *EVER* use RemovalGroup for your house removal. You'll
regret it forever.
http://thedoomedcity.blogspot.com/2010/03/removal-group-nightmare.html <==

Andrea Gavana

unread,
Jul 23, 2011, 7:20:21 PM7/23/11
to wxpytho...@googlegroups.com
Hi,

On 22 July 2011 00:33, Andrea Gavana wrote:
> On 15 July 2011 00:00, dtp lists wrote:
>> Andrea,
>>
>>     The UltimateListCtrl is exactly what I was looking for! Thank you so
>> much. One quick question regarding the column header height... is that
>> adjustable somehow? The native wx.ListCtrl has a very thin header while the
>> ultimatelistctrl has a slightly thicker one. There does not seem to be a
>> straightforward way to resize the header hight via the API.
>
> There isn't actually a way to do it, the header height is pulled out
> using wx.RendererNative:
>
> wx.RendererNative.Get().GetHeaderButtonHeight(listctrl)
>
> But it should be easy enough to add. If you can enter a bug report on
> wxTrac that would be most helpful, so that I don't lose track of this
> improvement (component=AGW).

No need for a bug report, I just committed a fix in SVN which allows
to set a custom value for the header and footer height
(independently). You can now simply use:

list_control.SetHeaderHeight(30)

And it will do the job.

Reply all
Reply to author
Forward
0 new messages