Solved

ListView not repainting

Posted on 2004-10-20
6
449 Views
Last Modified: 2010-07-27
I have a form that contains a ListView set to Detail view type. And when the form loads it runs the following code:

OdbcConnection conn = new OdbcConnection ("DSN=Data");
                        conn.Open();
                        OdbcCommand cmd = new OdbcCommand("SELECT Field1, Field2 FROM Table1",conn);

                        OdbcDataReader data= cmd.ExecuteReader();
                        lstViewUsers.Clear();
                   
                        while(data.Read()){
                              ListViewItem lvi=lstViewUsers.Items.Add(data.GetString(0));
                                OdbcConnection conn2 = new OdbcConnection("DSN=Data");
                                conn2.Open();
                                OdbcCommand cmd2=new OdbcCommand("SELECT Field1, Field2 Field3 FROM Table2 WHERE Field1 = '"+data.GetString(0)+"';",conn2);                                OdbcDataReader data2 = cmd2.ExecuteReader();

                                if(data2.HasRows){
                                      data2.Read();
                                      lvi.SubItems.Add(data2.GetString(0));
                                      lvi.SubItems.Add(data2.GetDate(1).ToString("g"));
                                      lvi.SubItems.Add(data2.GetDate(2).ToString("g"));
                                }
                                else {
                                      lvi.SubItems.Add("");
                                        lvi.SubItems.Add("");
                                        lvi.SubItems.Add("");
                                }
                                data2.Close();
                                conn2.Close();
                                if(data.GetBoolean(1))
                                      lvi.SubItems.Add("Yes");
                                else
                                      lvi.SubItems.Add("No");
                  }

                        data.Close();
                        conn.Close();

And as a result somehow the ListView Does not repaint itself (it also happened Else where in the program but the solution should be the same.
0
Comment
Question by:gilllyo
6 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 12357370
If you minimize your app, and then restore it, does the listview repaint itself correctly?
0
 
LVL 3

Expert Comment

by:aacool
ID: 12358227
Try putting in a call to listViewUsers.Update(); - ideally, start your function with
listViewUsers.BeginUpdate();  and end with listViewUsers.EndUpdate();

See if this works
0
 
LVL 15

Expert Comment

by:SRigney
ID: 12359091
listViewUsers.Refresh() may work as well.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
viola123 earned 125 total points
ID: 12366125
using
lstViewUsers.Items.Clear();
lstViewUsers.Columns.Clear();

before you send any items to listview.

i suggest you creat a method that used to display dataset in listview and then call this method whenever you need.
0
 

Author Comment

by:gilllyo
ID: 12366805
When I minimiz then come back all the headings are greyed out and there are no items in the list.

I put the headers in during design time so I don't want to clear the Headers

If you noticed I had the code:

lstViewUsers.Clear();

I changed this to:

lstViewUsers.Items.Clear();

And it displays what it should.

Thanks
0
 
LVL 6

Expert Comment

by:viola123
ID: 12367105
i am glad it helps

viola
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question