[wxruby-users] Grid#get_selected_rows not working?

Alex Fenton alex at pressure.to
Thu Mar 12 18:42:09 EDT 2009


Hi Trans

Trans wrote:
> Working with wxGrid. I can't seem to get any results for
> #get_selection_rows. 

Hmm, it (get_selected_rows, there's not a method get_selection_rows) 
seems to work OK to me. I tweaked the grid sample line 27 so it reads:

    evt_menu(1003) { @grid.select_row(1); p @grid.selected_rows }

And that returns [1].

However (any maybe this is where the confusion lies) Grid#selected_rows 
only returns rows that have been selected as such, ie by clicking on the 
row label, or programmatically with select_row.

Rows that have some cells, or even all cells selected by click-dragging 
won't be returned, because they're not 'selected-as-rows'. If that's 
what you need, then I guess you may have to resort to more complicated 
strategies:

>       rows = []  # @grid.get_selection_rows (not working)
>       top = @grid.get_selection_block_top_left.map{ |r,c| r }
>       bot = @grid.get_selection_block_bottom_right.map{ |r,c| r }
>       top.each_with_index do |t, i|
>         rows.concat((t..bot[i]).to_a)
>       end
>   

Note that if you want all rows where anything is selected, you'll also 
need to call get_selected_cells to get selections not in a block. See 
below for a suggested useful method

> Did I miss something or is this in fact a bug?

Not a bug, I think, but definitely not something very clear in the docs. 
And the API for multiple selections is not exactly comfortable. In 
another version we should probably add a simpler method like:

# Returns an array of co-ordinates of every selected cell.
def get_all_selected_cells
      cells = []
      selection_block_top_left.zip(selection_block_bottom_right) do | 
coords |
        x1, y1, x2, y2 = coords.flatten
        (x1..x2).each { | x | (y1..y2).each { | y | cells << [x, y] } }
      end
      selected_cells.each { | c | cells << c }
      cells.sort!
      cells
end

With this you could get all rows with any selection with just

  all_selected_cells.map { | c | c.first }.uniq

a


More information about the wxruby-users mailing list