Get selected rows of DataViewListCtrl

How to get selected rows of a DataViewListCtrl with style DV_MULTIPLE?

DataViewListCtrl.GetSelectedRow() only returns a single selected row.

DataViewCtrl.GetSelections() returns multiple rows, but of type DataViewItem, and the documentation seems to have no mentioning of how such objects should be used.

It’s probably too late for @champignoom, but I was looking at this issue for one of my own projects today.

The answer is to pass each DataViewItem to the DataViewListCtrl's ItemToRow() method, as shown in the example below:

import wx
import wx.dataview as dv

DATA = (
    (0, 100),
    (1, 127),
    (2, 116),
    (3, 80),
    (4, 108),
    (5, 77),
    (6, 90),
    (7, 22),
)


class TestFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        self.SetSize((150, 300))
        self.dvlc = dv.DataViewListCtrl(self, style=dv.DV_MULTIPLE|dv.DV_HORIZ_RULES|dv.DV_VERT_RULES)
        self.dvlc.AppendTextColumn('Step', width=40)
        self.dvlc.AppendTextColumn('Value')
        self.Sizer = wx.BoxSizer(wx.VERTICAL)
        self.Sizer.Add(self.dvlc, 1, wx.EXPAND)
        b = wx.Button(self, -1, "Get Selected Rows")
        self.Sizer.Add(b, 0, wx.TOP, 8)

        for d in DATA:
            self.dvlc.AppendItem([str(i) for i in d])

        self.Bind(wx.EVT_BUTTON, self.OnGetSelectedRows, b)

    def OnGetSelectedRows(self, _event):
        print([self.dvlc.ItemToRow(i) for i in self.dvlc.GetSelections()])


app = wx.App()
frame = TestFrame(None)
frame.Show()
app.MainLoop()

Tested using Python 3.10.6 + wxPython 4.2.0 gtk3 (phoenix) wxWidgets 3.2.0 on Linux Mint 21.1