# VisualRuby - How to clear the rows in listview

**URL:** <https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497>\
**Category:** Ruby-Gnome 2\
**Created:** [April 17, 2014, 5:31am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497 "2014-04-17T05:31:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [April 17, 2014, 5:31am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/1 "2014-04-17T05:31:41Z")

</div>

In my program, I need to display data which periodically changes, that  
means  
the number of rows and content in the listview completely change.

I would like to clear the rows in the listview and re-populate the  
information for the data source. The columns still remain the same.

Can you give me an example or snippet of code to clear the rows.

Thanks,  
John

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 17, 2014, 9:40pm UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/2 "2014-04-17T21:40:42Z")

</div>

I usually write a refresh() method that deletes all the records, then  
fills it in again. I’ve had no speed problems because I’ve never done  
huge tables.

You can use the clear() method on the model.

@listview.model.clear

Here is the code from visualruby where the “Choose Project” dialog box  
appears. The listview is populated with all the visualruby projects  
found on your hard disk:

def refresh(show\_backup = false)  
model.set\_sort\_column\_id(1)  
self.model.clear  
pattern = “”  
backup\_path = Dir.pwd.gsub(ENV[“HOME”], File.join(ENV[“HOME”],  
“visualruby\_backup”))  
backup\_ar = File.split(backup\_path)  
backup\_ar \<\< ["**", VR\_ENV::SETTINGS\_FILE]  
if show\_backup  
pattern = File.join(\*backup\_ar)  
else  
pattern = File.join(ENV[“HOME”], “visualruby”, "**",  
VR\_ENV::SETTINGS\_FILE)  
end  
Dir.glob(pattern, File::FNM\_DOTMATCH).each do |fn|  
next if not show\_backup and fn =~ / Backup /  
next if show\_backup and not fn =~ / Backup /  
t = File.stat(fn).mtime  
row = add\_row()  
row[:pix] = PIX  
row[:file\_name] = File.dirname(fn)  
row[:modified] = DateTime.parse(t.to\_s)  
end  
model.set\_sort\_column\_id(id(:modified), Gtk::SORT\_DESCENDING)  
end

Hope that helps,  
Eric

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 17, 2014, 10:43pm UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/3 "2014-04-17T22:43:50Z")

</div>

Eric,

The “self.model.clear” worked, it cleared out all of the lines in  
the listview and program was able to repopulate the list with new  
data.

What command is used to clear one line in the listview ( ie del 5th  
line)?

Is it possible to change the background of just one field in the  
listview,  
ie - row[:status]…

Thanks,  
John

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 18, 2014, 12:04am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/4 "2014-04-18T00:04:41Z")

</div>

Yes, you can change the background color. See the listview example  
program. Go to Open project. Look in the examples folder for the  
listview. It has a button to change the background to yellow.

If I remember correctly, its difficult to do individual cells, but there  
is a way.

Eric

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 17, 2014, 10:49pm UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/5 "2014-04-17T22:49:39Z")

</div>

Eric,

FYI: A listview with 4000 entries and 6 fields was able to clear  
with the “self.model.clear” command and repopulated  
with just a quick blink of the display (no pause). Impressive!!!

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 19, 2014, 7:00pm UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/6 "2014-04-19T19:00:17Z")

</div>

Hi Eric,

Looked at “listview” example, it changes the background color  
of the whole column.

I would like to change only the color of a specific item in the row,  
something like row[:name].setbackgroundcolor=“red”

Thank you for your help,  
John

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 22, 2014, 12:48am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/7 "2014-04-22T00:48:53Z")

</div>

I found this:

> <https://stackoverflow.com/questions/5171300/how-to-set-the-color-of-a-single-cell-in-a-pygtk-treeview>

Seems hokey. I don’t know the answer now. I think I remember looking  
at this issue, but I can’t remember the answer.

Eric

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [April 22, 2014, 1:01am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/8 "2014-04-22T01:01:50Z")

</div>

Ok I found it. When you define an object to be a column in a listview,  
you create a method called visual\_attributes(). This method returns a  
hash with the attributes in it. e.g. hash[:foreground] = “white”.

Look at this example from the active\_record2 project found in the  
examples folder:

class Employee \< ActiveRecord::Base  
has\_many :paychecks  
belongs\_to :employer

include GladeGUI

def visual\_attributes  
bal = balance().to\_f  
hash = { :text =\> name }  
hash[:foreground] = (bal \< 0) ? “white” : “black”  
hash[:background] = (bal \< 0) ? “red” : “white”  
return hash  
end

def balance()  
Paycheck.sum(:amount, :conditions =\> “employee\_id = #  
{id}”).round(2).to\_s

# Paycheck.find(“employee\_id = #{id}”).calculate(:sum,

:amount).round(2).to\_s  
end

end

This project isn’t working right now. I beleive its due to activerecord  
constantle changing their syntax on me. Its not summing the balance  
correctly. Perhaps someone can debug the balance() method for me. When  
the balance() method returns a negative value, the individual cell will  
appear red.

I think you’ll agree that this is a very good solution.

Eric

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 15, 2014, 4:29am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/9 "2014-12-15T04:29:44Z")

</div>

Thank you for your help!

This is a very good solution.

* * *

[http://betdownload.com](http://betdownload.com)

---

<div class="post-metadata">

**Author:** ![mateomiller](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/mateomiller/32/1254_2.png) [@mateomiller](https://www.ruby-forum.com/u/mateomiller)\
**Post date:** [December 7, 2019, 5:31am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/10 "2019-12-07T05:31:12Z")

</div>

Hi, have you checked this out [http://forums.asp.net/t/1328392.aspx/1](http://forums.asp.net/t/1328392.aspx/1)

---

<div class="post-metadata">

**Author:** ![gangotri\_11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5a626/32.png) [@gangotri\_11](https://www.ruby-forum.com/u/gangotri_11)\
**Post date:** [December 16, 2019, 4:21am UTC](https://www.ruby-forum.com/t/visualruby-how-to-clear-the-rows-in-listview/240497/11 "2019-12-16T04:21:24Z")

</div>

The following ruby forum resolves the technical issues through conversation.Thanks for the outstanding support and help.I have two queries that how I can build applications with  
Ruby and How could I use the new sign in page for [office 365 login](https://supportprop58.com/office-setup/how-to-login-microsoft-office/)?
