# Grid#get\_selected\_rows not working?

**URL:** <https://www.ruby-forum.com/t/grid-get-selected-rows-not-working/162688>\
**Category:** wxRuby\
**Created:** [March 12, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/grid-get-selected-rows-not-working/162688 "2009-03-12T20:00:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 12, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/grid-get-selected-rows-not-working/162688/1 "2009-03-12T20:00:21Z")

</div>

Working with wxGrid. I can’t seem to get any results for  
#get\_selection\_rows. The array is alwasy empty. I ended up having to  
something tricky:

```
  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

```

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

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 13, 2009, 12:18am UTC](https://www.ruby-forum.com/t/grid-get-selected-rows-not-working/162688/2 "2009-03-13T00:18:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 13, 2009, 9:08am UTC](https://www.ruby-forum.com/t/grid-get-selected-rows-not-working/162688/3 "2009-03-13T09:08:59Z")

</div>

Trans wrote:

> Hmm… that seems odd to me since I’m using row selection mode
> 
> @grid.set\_selection\_mode(1)
> 
> But yea, that seems to be the problem.

You didn’t mention that; it definitely seems odd to me that it works  
that way with the row grid select mode on. But it seems to be intended:

> **[wxTrac has been migrated to GitHub Issues - wxWidgets](https://wxwidgets.org/develop/how-to-report-issues/trac-migration/)**

[http://lists.wxwidgets.org/pipermail/wx-users/2007-February/097640.html](http://lists.wxwidgets.org/pipermail/wx-users/2007-February/097640.html)

You could file a bug/patch for wxWidgets to request a fix.

> > def get\_all\_selected\_cells

…

> Yes, something easier to use would be very helpful.

Well, feel free to make use of it as it stands … with complex widgets  
like Grid subclassing is often a neater way to organise code anyway.

cheers  
a

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 13, 2009, 1:42am UTC](https://www.ruby-forum.com/t/grid-get-selected-rows-not-working/162688/4 "2009-03-13T01:42:14Z")

</div>

On Thu, Mar 12, 2009 at 6:42 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â 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.

Hmm… that seems odd to me since I’m using row selection mode

@grid.set\_selection\_mode(1)

But yea, that seems to be the problem.

> > Â Â Â end  
> > version we should probably add a simpler method like:  
> > Â Â cells  
> > end
> 
> With this you could get all rows with any selection with just
> 
> Â all\_selected\_cells.map { | c | c.first }.uniq

Yes, something easier to use would be very helpful.

Thanks,  
T.
