Page 1 of 1

How to get selected rows of wxGrid?

Posted: Tue May 13, 2008 8:39 am
by bloodlee
Hi, all. I met another problem about wxGrid.

Now, when I left click the rows of wxGrid, I want to get how many rows has been selected. So I use:

Code: Select all

BEGIN_EVENT_TABLE(RecordGrid, wxGrid)
EVT_GRID_CELL_LEFT_CLICK(RecordGrid::OnLeftClick)
END_EVENT_TABLE()

Code: Select all

void RecordGrid::OnLeftClick(wxGridEvent& event)
{
      wxArrayInt rows = GetSelectedRows();
      .....
      event.Skip();
}
And in the function of OnLeftClick(), I used GetSelectedRows() to get the selected rows. But I found the wxArrayInt is not right, and I think the reason is I can not get the right rows before wxGrid, the base class, handles this event.

I really need to get the selected rows when the selection changes. Do you have any suggestions? Thanks.

Posted: Tue May 13, 2008 11:24 am
by JimFairway
Hi,

There are events that wxGrid generates when the selected cells are changed.

Here's the code snippet from the sample that demonstrates how to get the info you need:

Code: Select all

    EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
    EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
...

void GridFrame::OnSelectCell( wxGridEvent& ev )
{
    wxString logBuf;
    if ( ev.Selecting() )
        logBuf << _T("Selected ");
    else
        logBuf << _T("Deselected ");
    logBuf << _T("cell at row ") << ev.GetRow()
           << _T(" col ") << ev.GetCol()
           << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
           << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
           << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
           << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");

    //Indicate whether this column was moved
    if ( ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ) != ev.GetCol() )
        logBuf << _T(" *** Column moved, current position: ") << ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() );

    wxLogMessage( wxT("%s"), logBuf.c_str() );

    // you must call Skip() if you want the default processing
    // to occur in wxGrid
    ev.Skip();
}

void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
{
    wxString logBuf;
    if ( ev.Selecting() )
        logBuf << _T("Selected ");
    else
        logBuf << _T("Deselected ");
    logBuf << _T("cells from row ") << ev.GetTopRow()
           << _T(" col ") << ev.GetLeftCol()
           << _T(" to row ") << ev.GetBottomRow()
           << _T(" col ") << ev.GetRightCol()
           << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
           << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
           << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
           << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
    wxLogMessage( wxT("%s"), logBuf.c_str() );

    ev.Skip();
}
You could merge the two event handlers if you want and use
ev.GetEventType() to identify which event was raised.


Hope that helps,

Jim

Posted: Tue May 13, 2008 2:09 pm
by lester

Code: Select all

	wxArrayInt res;
	
	for( int i = 0 ; i < pGrid->GetNumberRows() ; ++i )
	{
		if( pGrid->IsInSelection( i, 0 ) )
			res.Add( i );
	}

Posted: Wed May 14, 2008 2:34 am
by bloodlee
Thanks, Jim and lester.

I've tried your methods, but the problem still exists. :(

Posted: Wed May 14, 2008 10:36 am
by JimFairway
Hi,

Which problem still exists?
Do you have the sample code: wx/samples/grid?
Based on your initial problem description, it looks like it shows how to do exactly what you want without having to use a
EVT_GRID_CELL_LEFT_CLICK event.


Jim

Posted: Thu May 15, 2008 12:57 am
by bloodlee
Hi,

I compiled the sample/grid and read the src. And I change the EVT_macro to EVT_GRID_SELECT_CELL and EVT_GRID_RANGE_SELECT.

But I can't get the right selected rows in the event handler, even I used the method which lester said.

Posted: Thu May 15, 2008 1:19 am
by JimFairway
Hi,

I'm presuming the sample worked. Can you post the code you're having trouble with?

Jim