Force Listbox to Refresh

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randy

    #1

    Force Listbox to Refresh

    I have button that deletes items from a SQL datatable. The items are
    displayed for the user in listbox, which is bound to the dataset. The
    code works just fine insofar as it deletes the correct record from the
    dataset and the datasource. What I need to make it do is to refresh
    the listbox so that it reflects that the record has been deleted.
    Right now, the listbox removes the first item from the list and leaves
    the deleted item displayed. If I close the form and reopen it, all
    appears as it should (e.g. the correct item is gone and the listbox
    displays correctly). How can I force the listbox to refresh as if the
    form were re-loading.?

    Here is the code:

    Dim strSQL As String
    strSQL = "DELETE Category WHERE Category = @Cat"
    Dim cmdDelete As New SqlCommand(strS QL, cn)
    cmdDelete.Param eters.AddWithVa lue("@Cat",
    lbCat.SelectedV alue)

    Try
    cn.Open()
    Dim intRecordsAffec ted As Integer
    intRecordsAffec ted = cmdDelete.Execu teNonQuery()
    If intRecordsAffec ted = 1 Then
    CategoryBinding Source.RemoveCu rrent()
    CategoryBinding Source.MoveFirs t()

    End If
    Catch ex As Exception
    MessageBox.Show (ex.Message)
    Finally
    cn.Close()
    End Try

    Thanks a lot,
    Randy

  • Cor Ligthert [MVP]

    #2
    Re: Force Listbox to Refresh

    Randy mostly is this the most simple one

    listbox.datasou rce = nothing
    listbox.datasou rce = thedatasource

    Cor

    "Randy" <spam.eastland@ gmail.comschree f in bericht
    news:1179442446 .961594.303180@ y80g2000hsf.goo glegroups.com.. .
    >I have button that deletes items from a SQL datatable. The items are
    displayed for the user in listbox, which is bound to the dataset. The
    code works just fine insofar as it deletes the correct record from the
    dataset and the datasource. What I need to make it do is to refresh
    the listbox so that it reflects that the record has been deleted.
    Right now, the listbox removes the first item from the list and leaves
    the deleted item displayed. If I close the form and reopen it, all
    appears as it should (e.g. the correct item is gone and the listbox
    displays correctly). How can I force the listbox to refresh as if the
    form were re-loading.?
    >
    Here is the code:
    >
    Dim strSQL As String
    strSQL = "DELETE Category WHERE Category = @Cat"
    Dim cmdDelete As New SqlCommand(strS QL, cn)
    cmdDelete.Param eters.AddWithVa lue("@Cat",
    lbCat.SelectedV alue)
    >
    Try
    cn.Open()
    Dim intRecordsAffec ted As Integer
    intRecordsAffec ted = cmdDelete.Execu teNonQuery()
    If intRecordsAffec ted = 1 Then
    CategoryBinding Source.RemoveCu rrent()
    CategoryBinding Source.MoveFirs t()
    >
    End If
    Catch ex As Exception
    MessageBox.Show (ex.Message)
    Finally
    cn.Close()
    End Try
    >
    Thanks a lot,
    Randy
    >

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Force Listbox to Refresh

      "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
      Randy mostly is this the most simple one
      >
      listbox.datasou rce = nothing
      listbox.datasou rce = thedatasource
      .... which can be written as 'ListBox1.DataS ource = TheDataSource' too. It's
      not necessary to set the property to 'Nothing' there.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Force Listbox to Refresh

        Herfried,

        You are right, I thought that I had a bad expirience in that, but accoording
        all my writing about setting to nothing it would be crazy.

        Cor

        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
        news:uXMLMRXmHH A.4132@TK2MSFTN GP02.phx.gbl...
        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
        >Randy mostly is this the most simple one
        >>
        >listbox.dataso urce = nothing
        >listbox.dataso urce = thedatasource
        >
        ... which can be written as 'ListBox1.DataS ource = TheDataSource' too.
        It's not necessary to set the property to 'Nothing' there.
        >
        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Randy

          #5
          Re: Force Listbox to Refresh

          OK, as simple as this looks, it isn't working for me. Since my
          original post, I have changed my databinding so that I am now binding
          the listbox to a dataview (so that I can sort the data in the
          listbox). Here is my current code, which is intended to incorporate
          your suggestions:

          Dim strSQL As String
          strSQL = "DELETE Category WHERE Category = @Cat"
          Dim cmdDelete As New SqlCommand(strS QL, cn)
          cmdDelete.Param eters.AddWithVa lue("@Cat",
          lbCat.SelectedV alue)

          Try
          cn.Open()
          Dim intRecordsAffec ted As Integer
          intRecordsAffec ted = cmdDelete.Execu teNonQuery()
          If intRecordsAffec ted = 1 Then
          Dim vueCat As New DataView
          With vueCat
          .Table = ds.Category 'ds is my dataset
          .Sort = "Category"
          .RowStateFilter = DataViewRowStat e.CurrentRows
          End With

          With lbCat
          .DataSource = vueCat
          .DisplayMember = "Category"
          .ValueMember = "Category"
          End With
          End If
          Catch ex As Exception
          MessageBox.Show (ex.Message)
          Finally
          cn.Close()
          End Try

          I think that my problem has something to do with the
          DataRowViewStat e. I've played around with this setting and achieved
          different results. However, none of the options works in the various
          scenarios that I need to update the binding with (Insert, Delete,
          Update).

          Any advice is appreciated.
          Randy

          Comment

          • Randy

            #6
            Re: Force Listbox to Refresh

            Just to close out this post, I resolved the issue by simply filling my
            dataadapter. All the stuff inside the If-Then statement is gone,
            replaced with da.fill.

            Thanks for the help, Cor and Herfried.

            Comment

            Working...