Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Proper way to refresh a listview after doubleclick

Dear Experts,

I’m having an issue with my listview refreshing data when the data has been updated.   Currently I have a listview that displays the Status of whether a file cabinet has been used or not.  

Cabinet – Status
Cabinet1, Used
Cabinet2, Available
Cabinet3, etc….

Problem occurs is when I use a pop up form instigated by double clicking the listview,  I change the status of the file cabinet, but the same information still remains there.  I know that the status is being changed correctly in the database, but when I try to pull the new status, it doesn’t “refresh” correctly.  If I stop and start the program, no problems or if I use a combobox to only select those Cabnets that are available or not.   The only thing that has worked has been putting a sleep command of about a second between sending the new status and getting the new one.  I’m hoping for a better solution.  
0
carmodyk
Asked:
carmodyk
  • 7
  • 6
5 Solutions
 
RJeyaPrakashCommented:
hai

The problem is that the listview is already filled with the old data. U r not refreshing it with the new data...

After an update, refill yr dataset and bind it to listview.

Can u post yr code so that v can see what u did in yr code???
0
 
anyoneisCommented:
I think the problem is that the pop-up form and the listview are not sharing the same currency manager. When you instantiate the popup, try making its binding context the same as the form that is creating it.

Like:

   Dim Foo as MyPopUpForm = new MyPopUpForm()
 Foo.BindingContext = Me.BindingContext
  Foo.ShowDlg()


David
0
 
carmodykAuthor Commented:
First, I'm not using a dataset, but maybe that would be the way to go, I will give that a try.  And the problem is not binding the pop-up form to the "parent form" I already have that established.  

0
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!

 
anyoneisCommented:
Well, it really sounds like I was on with my suggestion... can you ensure that in the child form, (this.BindingContext == this.parent.BindingContext) is true? What if you use a the combo box but run it on the child form?

You are binding the Listview to some collection? What kind of collection? Some collections handle automatically informing bindees and some don't.

David
0
 
carmodykAuthor Commented:
Using a dataset doesn't help.  Here is my code:

Private Sub lvMain_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvMain.DoubleClick

        Dim selecteditem, strValue, strReturn As String
        Dim strStatus() As String
        Dim intFound As Integer

        Dim blnOK As Boolean

        Try
         'Using Case statement unless client wants more options

            Select Case lvMain.SelectedItems(0).SubItems(3).Text

                Case "Used"
                   Dim diaRes As Windows.Forms.DialogResult
                    diaRes = MessageBox.Show("Change Status to Available?", "File Cabinet", MessageBoxButtons.YesNo)
                    If diaRes = DialogResult.Yes Then
                        if not AddNewStatus("Available", lvMain.SelectedItems(0).SubItems(0).Text)
                        ChangeListviewMain()
                        End if
                    End If

                Case "Available"
                    Dim diaRes As Windows.Forms.DialogResult
                    diaRes = MessageBox.Show("Change Status to Used?", "Reservation", MessageBoxButtons.YesNo)
                    If diaRes = DialogResult.Yes Then
                   if not AddNewStatus("Available", lvMain.SelectedItems(0).SubItems(0).Text) = "" then
                    ChangeListviewMain()
                  End if
                    End if
            End Select

Private Function AddNewStatus(ByVal strStatus As String, ByVal strID as string)

        Dim cnSQL As SqlConnection
        Dim cmSQL As SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
        '  Dim datStart, datFinish As DateTime

        Try
            strSQL = "INSERT INTO tblFileCabinetStatus (FileCabinetID, TransDateTime, " & _
            "StatusID) VALUES ("& "," & PrepareStr(strID) & ","  & PrepareStr(Now().ToString) & "," & PrepareStr(strStatus) & "); SELECT @@IDENTITY"

            cnSQL = New SqlConnection(m_Connection)
            cnSQL.Open()


            cmSQL = New SqlCommand(strSQL, cnSQL)

            Return cmSQL.ExecuteScalar().ToString

            ' Close and Clean up objects
            cnSQL.Close()
            cmSQL.Dispose()
            cnSQL.Dispose()

        Catch Exp As SqlException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "Add New Entry SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "Add New Entry General Error")
        End Try
    End Function

 Private Sub ChangeListviewMain()

        Dim cnSQL As SqlConnection
        Dim cmSQL As SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
       
        Try
            lvMain.BeginUpdate()
            lvMain.Items.Clear()
           

            strSQL = "SELECT FileCabinetID, Max(TransDateTime) as LastTime, Max(Status) as lastStatus " & _
                  "FROM tblFileCabinetStatus GROUP BY FileCabinetID ORDER BY FileCabinetID"


            cnSQL = New SqlConnection(m_Connection)
            cnSQL.Open()

            cmSQL = New SqlCommand(strSQL, cnSQL)
            drSQL = cmSQL.ExecuteReader()
           
            While drSQL.Read()
             
                strlv1 = drSQL.Item("FileCabinetID").ToString() & "," & _
                strlv2 = drSQL.Item("LastTime").ToString() & "," & _
                strlv3 = drSQL.Item("lastStatus").ToString() & "," & _
   

                Dim lstItem As New ListViewItem(strlv1)
                lstItem.SubItems.Add(strlv2)
            lstItem.SubItems.Add(strlv3)
                lvMain.Items.Add(lstItem)
            End While

            lvMain.EndUpdate()

        Catch exp As SqlException
            MsgBox(exp.Message, MsgBoxStyle.Critical, "Index Changed SQL Error")

        Catch exp As Exception
            MsgBox(exp.Message, MsgBoxStyle.Critical, "Index Changed General Error")
        Finally
            ' Close and Clean up objects
            drSQL.Close()
            cnSQL.Close()
            cmSQL.Dispose()
            cnSQL.Dispose()
        End Try
    End Sub
0
 
anyoneisCommented:
OK - I was wayyy off! :-)

I'm not a VB guru, but the return value from AddNewStatus is bothering me - if no records are returned you will throw an exception when you try to evaluate Nothing.ToString(). (it's probably code-thrash but I thought I would mention it.)

So I would return it like: Return cmSQL.ExecuteScalar()

And check it as you have in the first of the cases, not the second.

But, back to the problem, are you sure ChangeListviewMain is being called? Have you stepped through this in the debugger?

Other experts!?!?!?! Are you out there?

David

0
 
carmodykAuthor Commented:
Yep ChangeListViewMain is always called, If I add a Initialize.Sleep(1000) between the insert and the select portion of the calls, I have no problems.  Problem is, I don't want to wait that long for a change to commence.

0
 
anyoneisCommented:
Hmm. What about Sleep(2)?
0
 
carmodykAuthor Commented:
I've dialed down the initialize.Sleep function, but it's only consistant when it's up at the 1000 level.

0
 
anyoneisCommented:
Is it a "paint" issue? That is, when the listview doesn't update can you cause it to update by swithcing windows, min-maxing, ...?

0
 
carmodykAuthor Commented:
No, not a paint issue.  When I do the min-Max thing or switch windows, it remains not changed.  
0
 
anyoneisCommented:
Where is this Initialize.Sleep() method? I can't find anything about that. Also, where exactly is that statement going?

0
 
carmodykAuthor Commented:
I didn't post it with my code, as I was looking for an answer that wouldn't involve it.  But, if I place it after I do the Insert portion of the program, then it works.

 Case "Used"
                   Dim diaRes As Windows.Forms.DialogResult
                    diaRes = MessageBox.Show("Change Status to Available?", "File Cabinet", MessageBoxButtons.YesNo)
                    If diaRes = DialogResult.Yes Then
                        if not AddNewStatus("Available", lvMain.SelectedItems(0).SubItems(0).Text) = "" then
'Here is the Initalize sleep
                       Thread.Sleep(1000)
                        ChangeListviewMain()
                     End if
                    End If
0
 
carmodykAuthor Commented:
Guess what gang, I've found the problem and it wasn't a refresh problem, but it was a SQL Problem.  I have however moved some of the data that I was querying for into a single datatable and then I refresh the listview based off of that, rather than refreshing the listview with two queries back to back.  

I will try to dispurse some points to you guys for the help.  Thanks again,
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now