Solved

ListView not repainting

Posted on 2004-10-20
6
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
using sqldatareader to populate cells in Excel-syntax 6 33
Web page design problem 3 41
Timeouts during development 3 28
Need a quick overview of Selenium testing 2 35
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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