# Problem inserting row into a grid

**URL:** <https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545>\
**Category:** wxRuby\
**Created:** [January 2, 2013, 7:31pm UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545 "2013-01-02T19:31:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![norbertl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c77e96/32.png) [@norbertl](https://www.ruby-forum.com/u/norbertl)\
**Post date:** [January 2, 2013, 7:31pm UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545/1 "2013-01-02T19:31:40Z")

</div>

I’m having a bit of a problem inserting a row (or column) into a grid.  
I’m using my own custom GridTableBase and created override methods for  
insert\_rows, insert\_cols, get\_number\_rows and get\_number\_cols. The  
problem I’m having is that calling the grid.insert\_rows method does not  
properly update the number of rows displayed, and the last row of the  
table disappears into oblivion even though it still exists in the  
underlying GridTableBase.

The attached code is a simplified version of my code that should  
illustrate the problem I’m having.

Thanks,  
Norbert

---

<div class="post-metadata">

**Author:** ![norbertl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c77e96/32.png) [@norbertl](https://www.ruby-forum.com/u/norbertl)\
**Post date:** [January 3, 2013, 3:07am UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545/2 "2013-01-03T03:07:11Z")

</div>

Hi Norbert

On 02/01/13 18:31, Norbert Lange wrote:

> I’m having a bit of a problem inserting a row (or column) into a grid.  
> I’m using my own custom GridTableBase and created override methods for  
> insert\_rows, insert\_cols, get\_number\_rows and get\_number\_cols. The  
> problem I’m having is that calling the grid.insert\_rows method does not  
> properly update the number of rows displayed, and the last row of the  
> table disappears into oblivion even though it still exists in the  
> underlying GridTableBase

I think I ran into a similar problem a while ago. I solved it by  
re-calling the set\_table method, which causes the Grid to inspect the  
DataSource again to determine the number of rows and columns.

See the sub-classed refresh method in  
[http://weft-qda.rubyforge.org/svn/trunk/weft-qda/lib/weft/wxgui/controls/codereview\_grid.rb](http://weft-qda.rubyforge.org/svn/trunk/weft-qda/lib/weft/wxgui/controls/codereview_grid.rb)

best  
alex

---

<div class="post-metadata">

**Author:** ![norbertl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c77e96/32.png) [@norbertl](https://www.ruby-forum.com/u/norbertl)\
**Post date:** [January 3, 2013, 5:51pm UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545/3 "2013-01-03T17:51:54Z")

</div>

Hi Alex,

I gave your suggestion a try but it results in a segmentation fault when  
I try to set the table for a second time, even if I do it immediately  
after the first grid.set\_table before any manipulation of the table.

C:/Ruby187/lib/ruby/gems/1.8/gems/wxruby-2.0.1-x86-mingw32/lib/wx/classes/grid.rb:34:  
[BUG] Segmentation fault  
ruby 1.8.7 (2011-12-28 patchlevel 357) [i386-mingw32]

Any other thoughts would be appreciated.

Regards,  
Norbert

---

<div class="post-metadata">

**Author:** ![norbertl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c77e96/32.png) [@norbertl](https://www.ruby-forum.com/u/norbertl)\
**Post date:** [January 5, 2013, 6:13pm UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545/4 "2013-01-05T18:13:13Z")

</div>

On 05/01/2013 00:26, Norbert Lange wrote:

> After another bit of experimenting, I’ve found that if I create a new  
> grid and then use the grid.create\_grid method() to create a table that  
> inserting rows into that table works. What I’m concluding is that I’ve  
> implemented something incorrectly in my SpreadsheetTable class or  
> perhaps missed implementing a virtual function properly.  
> I don’t think so; see this on the C++ version:  
> [http://stackoverflow.com/questions/8857610/refreshing-wxgrid-with-dynamic-contents](http://stackoverflow.com/questions/8857610/refreshing-wxgrid-with-dynamic-contents)

Does calling force\_refresh() help? It appears to be available in wxRuby.

alex

---

<div class="post-metadata">

**Author:** ![norbertl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c77e96/32.png) [@norbertl](https://www.ruby-forum.com/u/norbertl)\
**Post date:** [January 5, 2013, 12:26am UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545/5 "2013-01-05T00:26:08Z")

</div>

After another bit of experimenting, I’ve found that if I create a new  
grid and then use the grid.create\_grid method() to create a table that  
inserting rows into that table works. What I’m concluding is that I’ve  
implemented something incorrectly in my SpreadsheetTable class or  
perhaps missed implementing a virtual function properly.

Again, any suggestions anyone may have would be appreciated.

Regards,  
Norbert

---

<div class="post-metadata">

**Author:** ![norbertl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c77e96/32.png) [@norbertl](https://www.ruby-forum.com/u/norbertl)\
**Post date:** [January 7, 2013, 5:14pm UTC](https://www.ruby-forum.com/t/problem-inserting-row-into-a-grid/229545/6 "2013-01-07T17:14:13Z")

</div>

Hi Alex,

Calling force\_refresh doesn’t seem to help. The problem seems to be  
centered around the grid not properly updating after the underlying  
table is changed. The following code snippet illustrates I think that  
the grid is not calling the gtb get\_number\_rows method except perhaps  
during the initial grid.set\_table call.

gtb = SpreadsheetTable.new([“A”,“B”], [“10”,“20”], [[1,2],[3,4]])  
grid = Wx::Grid.new(panel, -1)  
grid.set\_table(gtb)  
grid.insert\_rows(1,1)  
grid.force\_refresh

# Grid thinks there are still just 2 rows

puts grid.get\_number\_rows

# Correctly indicates 3 rows

puts gtb.get\_number\_rows

Regards,  
Norbert
