How to handle multiple dataview selections?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
apoorv569
I live to help wx-kind
I live to help wx-kind
Posts: 165
Joined: Tue Oct 20, 2020 3:35 pm

How to handle multiple dataview selections?

Post by apoorv569 » Wed Apr 07, 2021 8:44 am

I have a wxMenu for handling certain actions on a single wxDataViewListCtrl row. I want to be able to select multiple rows, and perform the action that is being clicked on the wxMenu for all the selected rows, but I can't seem to find a method to get index for all the selected rows, for example if there are 20 items in the dataview, and 4,8,15 and 17 are selected I want to be able to get these indexes, so can I call a for loop for these indexes or something with GetSelectedItemsCount() as a condition perhaps.

User avatar
doublemax
Moderator
Moderator
Posts: 15827
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to handle multiple dataview selections?

Post by doublemax » Wed Apr 07, 2021 10:07 am

Look in the base class, there is wxDataViewCtrl::GetSelections
https://docs.wxwidgets.org/trunk/classw ... b254c41c86
Use the source, Luke!

apoorv569
I live to help wx-kind
I live to help wx-kind
Posts: 165
Joined: Tue Oct 20, 2020 3:35 pm

Re: How to handle multiple dataview selections?

Post by apoorv569 » Wed Apr 07, 2021 10:56 am

doublemax wrote:
Wed Apr 07, 2021 10:07 am
Look in the base class, there is wxDataViewCtrl::GetSelections
https://docs.wxwidgets.org/trunk/classw ... b254c41c86
I see, but what is wxDataViewItemArray here? Do I have to make that array myself? If so what will be its value? Because just doing, ( I selected 0, 2 and 4th row)

Code: Select all

     wxDataViewItemArray row_array;
     int row = m_DVLC->GetSelections(row_array);

     wxLogDebug("Value of row: %d", row);
Gives me,

Code: Select all

16:19:56: Debug: Value of row: 3
16:19:56: Debug: Value of row: 3
16:19:56: Debug: Value of row: 3
As 3 was the last row I selected.

User avatar
doublemax
Moderator
Moderator
Posts: 15827
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to handle multiple dataview selections?

Post by doublemax » Wed Apr 07, 2021 11:23 am

From the "dataview" sample, code to delete all seletected items:

Code: Select all

void MyFrame::DeleteSelectedItems()
{
    wxDataViewItemArray items;
    int len = m_ctrl[Page_Music]->GetSelections( items );
    for( int i = 0; i < len; i ++ )
        if (items[i].IsOk())
            m_music_model->Delete( items[i] );
}
Use the source, Luke!

apoorv569
I live to help wx-kind
I live to help wx-kind
Posts: 165
Joined: Tue Oct 20, 2020 3:35 pm

Re: How to handle multiple dataview selections?

Post by apoorv569 » Wed Apr 07, 2021 11:59 am

doublemax wrote:
Wed Apr 07, 2021 11:23 am
From the "dataview" sample, code to delete all seletected items:

Code: Select all

void MyFrame::DeleteSelectedItems()
{
    wxDataViewItemArray items;
    int len = m_ctrl[Page_Music]->GetSelections( items );
    for( int i = 0; i < len; i ++ )
        if (items[i].IsOk())
            m_music_model->Delete( items[i] );
}
I see, I'm try to change toggle column value based on the selected rows, and items cannot be used in GetToggleValue(), how would I get the individual rows, as doing this gives error,

Code: Select all

    wxDataViewItemArray items;
    int rows = m_SampleListView->GetSelections(items);

    for (int i = 0; i < rows; i++)
        {
            if (m_SampleListView->GetToggleValue(items[i], 0))
                m_SampleListView->SetToggleValue(false, items[i], 0);
            else
                m_SampleListView->SetToggleValue(true, items[i], 0);
        }

User avatar
doublemax
Moderator
Moderator
Posts: 15827
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to handle multiple dataview selections?

Post by doublemax » Wed Apr 07, 2021 12:05 pm

Use the source, Luke!

Post Reply