Problems with finding selection in a Grid

Hi,
I am trying to use cut and paste in a grid...

Regrettably I am having problems finding the "selection".

My app uses a table driven grid.

In the definition of the table object I have the following:
         # we need to do this before the call to autoresizecols
         self.grid.SetTable(self,False)
         self.grid.SetSelectionMode(wx.grid.Grid.SelectRows)

The definition of the grid look like ( method list abreviated...)
class MyGrid(wx.grid.Grid):
     def __init__(self,parent):
         wx.grid.Grid.__init__(self,parent)
         self.lastPopup = None
         self.popupmenu = wx.Menu()
         for text in ["View As Text Field", "View As Binary Image","Export Field..."] :
             item = self.popupmenu.Append(-1,text)
             self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)
         #self.GetGridWindow().Bind(wx.EVT_CONTEXT_MENU,self.OnShowPopup)
         self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)
         self.Bind(wx.EVT_CHAR, self.OnChar)

     def OnChar(self,evt):
         foo=evt.GetKeyCode()
         bar = evt.CmdDown()
         baz = self.IsSelection()
         if sys.platform=='darwin' : code = 99 # 'c'
         else: code = 3 # windows and unix
         if (evt.GetKeyCode() == code) and evt.CmdDown() and self.IsSelection():
             rows = self.GetSelectedRows()
             wx.MessageBox("rows: "+str(rows))
             return
         evt.Skip(True)

When I populate the grid and click on a row ( not the row label) and
do a cmd-c the dialog in OnChar shows row to be an empty list.ie even though
the row is highlighted and IsSelection() is true, rows = empty list.

If I click on a row label and then do a cmd-c I get the row number-1 in the
list, the correct answer.

If I click on a row header and shift click on another row header, everything between
the two rows appears to be selected. When I cmd-c, again rows is set to the empty
list.

If I click on a row header and cmd-click on another row header then the additional row
appears to be added to the selection. This time I will get the two correct row numbers
in the list when I do a cmd-c.

Questions:
   when I click on an actual row ( not the row header ) it appears to be selected
   IsSelection returns true but GetSelectedRows() fails. Why?

   When I click on a row header and then shift click on a different row header, it
   appears that all of the rows between the two clicked on rows are selected and
   IsSelection returns true but GetSelectedRows() returns an empty list. Why?

   Is there anyway I can force only one row to be selected at a time?

Thanks,

Jerry

Hi Jerry,

Take a look at the wxPyWiki entry for wx.grid, especially the section regarding selection management:

wxpywiki: wx.grid

Apparently everybody runs into a similar problem to what you’re experiencing; I did too. Because wx.Grid needs to do a good job at both allowing individual cells, entire rows, entire columns, and arbitrary ranges (and even non-contiguous groups) of each – as it turns out, the Grid class is simply not able to give an entirely simple answer when you ask which rows are selected. You need to both ask which rows are selected AND which cells are selected, then run through a loop that essentially takes the union of both of those things to get a useable selection. Keep searching the posts in this forum and you’ll find several that deal with this, including useable code that does that loop for you.

Good luck!

···

On 6/9/07, Jerry LeVan jerry.levan@gmail.com wrote:

Hi,
I am trying to use cut and paste in a grid…

Regrettably I am having problems finding the “selection”.

My app uses a table driven grid.

In the definition of the table object I have the following:

     # we need to do this before the call to autoresizecols
     self.grid.SetTable(self,False)
     self.grid.SetSelectionMode(wx.grid.Grid.SelectRows)

The definition of the grid look like ( method list abreviated…)

class MyGrid(wx.grid.Grid):
def init(self,parent):
wx.grid.Grid.init(self,parent)
self.lastPopup = None
self.popupmenu = wx.Menu()
for text in [“View As Text Field”, "View As Binary

Image",“Export Field…”] :
item = self.popupmenu.Append(-1,text)
self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)
#self.GetGridWindow().Bind
(wx.EVT_CONTEXT_MENU
,self.OnShowPopup)
self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)
self.Bind(wx.EVT_CHAR, self.OnChar)

 def OnChar(self,evt):
     foo=evt.GetKeyCode()
     bar = evt.CmdDown()
     baz = self.IsSelection()
     if sys.platform=='darwin' : code = 99  # 'c'
     else: code = 3 # windows and unix
     if (evt.GetKeyCode() == code) and evt.CmdDown

() and
self.IsSelection():
rows = self.GetSelectedRows()
wx.MessageBox("rows: "+str(rows))
return
evt.Skip(True)

When I populate the grid and click on a row ( not the row label) and

do a cmd-c the dialog in OnChar shows row to be an empty list.ie even
though
the row is highlighted and IsSelection() is true, rows = empty list.

If I click on a row label and then do a cmd-c I get the row number-1

in the
list, the correct answer.

If I click on a row header and shift click on another row header,
everything between
the two rows appears to be selected. When I cmd-c, again rows is set
to the empty

list.

If I click on a row header and cmd-click on another row header then
the additional row
appears to be added to the selection. This time I will get the two
correct row numbers
in the list when I do a cmd-c.

Questions:
when I click on an actual row ( not the row header ) it appears to
be selected
IsSelection returns true but GetSelectedRows() fails. Why?

When I click on a row header and then shift click on a different

row header, it
appears that all of the rows between the two clicked on rows are
selected and
IsSelection returns true but GetSelectedRows() returns an empty
list. Why?

Is there anyway I can force only one row to be selected at a time?

Thanks,

Jerry


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org

For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Jerry LeVan wrote:

Hi,
I am trying to use cut and paste in a grid...

Regrettably I am having problems finding the "selection".

My app uses a table driven grid.

In the definition of the table object I have the following:
        # we need to do this before the call to autoresizecols
        self.grid.SetTable(self,False)
        self.grid.SetSelectionMode(wx.grid.Grid.SelectRows)

The definition of the grid look like ( method list abreviated...)
class MyGrid(wx.grid.Grid):
    def __init__(self,parent):
        wx.grid.Grid.__init__(self,parent)
        self.lastPopup = None
        self.popupmenu = wx.Menu()
        for text in ["View As Text Field", "View As Binary
Image","Export Field..."] :
            item = self.popupmenu.Append(-1,text)
            self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)
        #self.GetGridWindow().Bind(wx.EVT_CONTEXT_MENU,self.OnShowPopup)
        self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)
        self.Bind(wx.EVT_CHAR, self.OnChar)

    def OnChar(self,evt):
        foo=evt.GetKeyCode()
        bar = evt.CmdDown()
        baz = self.IsSelection()
        if sys.platform=='darwin' : code = 99 # 'c'
        else: code = 3 # windows and unix
        if (evt.GetKeyCode() == code) and evt.CmdDown() and
self.IsSelection():
            rows = self.GetSelectedRows()
            wx.MessageBox("rows: "+str(rows))
            return
        evt.Skip(True)

For a selection of multiple cells/rows/labels, i.e a 'range select' you need to
catch a wx.grid.EVT_GRID_RANGE_SELECT. Have a look here:

http://wiki.wxpython.org/wxGrid?highlight=(grid)#head-fbd9c2edf9265d3730aa8f9ccb9bd3e3ea798a19

Christian