none
List box data bindings and clearing them

    Question

  • After I pull information into the listbox, from the database, the operations no longer populate the text boxes when double clicked on. If the listbox is populated simply from doing calculations and saving them, then they can be clicked on. There is no error, simply nothing happens. I have tried using ListBox1.DataBindings.Clear() which doesn't seem to do anything, and ListBox1.DataSource.Clear and ListBox1.DataSource = Nothing, which simply causes the retrieve button to not work since as I understand it, it clears the source, which is populating the listbox.

    Imports System.Data.OleDb
    Module Module1
    
        'global public variable
        Public myDataSet As DataSet
        'global public method
        Public Sub GetData()
            Dim cn As OleDbConnection = New OleDbConnection(" Provider=Microsoft.ACE.OLEDB.12.0;" +
                          "Data Source=|DataDirectory|\CalcDB.accdb")
            Dim cmd As OleDbCommand = New OleDbCommand("SELECT TOP 10 Operation FROM CalcOps", cn)
            cn.Open()
    
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            myDataSet = New DataSet()
            myDA.Fill(myDataSet, "CalcOps")
    
            cn.Close()
            cn = Nothing
        End Sub
    End Module
    
    
    Private Sub ButtonRet_Click(sender As Object, e As EventArgs) Handles ButtonRet.Click
    
            Try
                
                Call Module1.GetData()
    
    
                If myDataSet.Tables.Count > 0 And myDataSet.Tables(0).Rows.Count > 0 Then
                    ListBox1.DataSource = myDataSet.Tables(0)
                    ListBox1.DisplayMember = "Operation"
    
                End If
    
    
    
    
                'Shows an error message with the problem message
            Catch ex As Exception
                MessageBox.Show("Error: " + ex.Message)
            End Try
    
        End Sub


    Thursday, January 09, 2014 9:14 PM

Answers

All replies

  • Hi WarMacheen

    listBox1.DataSource = Nothing  will not clear the source, it will only clear the listbox1 and at any point you can do ListBox1.DataSource = myDataSet.Tables(0) which will re-populate the listbox.

    Friday, January 10, 2014 9:48 AM
  • Thanks, I am still wondering about why the listbox is still not allowing the double click. I understand it is bound, which is why it shows the info from the database, but if I clear the datasource, then the listbox shows nothing.
    Friday, January 10, 2014 9:03 PM
  • I'm confused. Why you are associating double click with data bind and data clear. Double click is independent of data binding.

    I may help you more if you can state your problem clearly and provide more details.

    Saturday, January 11, 2014 3:36 PM
  • Don't create a new DataSet everytime you retrieve new data. Only create it once and then clear and re-fill it.

    Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
    If (myDataSet Is Nothing) Then
        myDataSet = New DataSet()
    Else
        myDataSet.Clear()
    End If
    
    myDA.Fill(myDataSet, "CalcOps")

    That way you don't "break" the DataSource.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 13, 2014 5:39 AM
  • Hi WarMacheen, 

    >>I am still wondering about why the listbox is still not allowing the double click.

    If you want double click, you can design as this thread mentioned:

    http://stackoverflow.com/questions/4454423/c-sharp-listbox-item-double-click-event

    >>I understand it is bound, which is why it shows the info from the database, but if I clear the datasource, then the listbox shows nothing.

    As "Jeetendra Khandelwal" mentioned, double click is different from databinding so I don't think they are related. As for binding, you've bind it with your datasource, if you clear your datasource, it is true that listbox will show nothing for you.

    Regards,

     



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 10:14 AM