Hi!!!
I'm having hard time in manipulating a combo box with list view. For example if the selected index in the combo box is "boy" then the list view will display a database table allotted for boys and if the selected index is "girl" then the list view will display the database table allotted for girls.

I have this codes for adding items in the combo box and placed it in Form1_Load:

comBoxYLevel.Items.Add("Boy")
	comBoxYLevel.Items.Add("Girl")

I'm using this codes to select the database:

Private Sub retBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles retBtn.Click

        Select Case comBoxYLevel.SelectedIndex
            Case 0
                strsql = "select * from boys"
            Case 1
                strsql = "select * from girls"
        End Select
       
 fillListView()
    End Sub

Unfortunately, the result of these codes is: the list view will successfully display the first selected case but once I select another case from the combo box, the first selected case is still there together with the second one, and so on. I want to display only what is selected, not including the previous one... How will I do it?
any response is well appreciated :)

before loading the data into the listbox you need to call the .Clear() method on the listbox. This will remove the current data from the listbox. Without that it just appends the new data to what is already in there.

thanks for your response, hericles...
when I used the .clear() method, even the column headers were cleared. I do it this way

Private Sub retBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles retBtn.Click

Select Case comBoxYLevel.SelectedIndex
Case 0
strsql = "select * from boys"
Case 1
strsql = "select * from girls"
End Select

subjListView.Clear()
fillListView()
End Sub

--wherein subjListView is the name of my list view

Try ListView1.Items.Clear() instead of ListView1.Clear(). That will clear the items only, not the column headers.

thanks for your response, reverend jim... but still appends...

I did this one:

Private Sub retBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles retBtn.Click

Select Case comBoxYLevel.SelectedIndex
Case 0
strsql = "select * from boys"
Case 1
strsql = "select * from girls"
End Select

subjListView.items.Clear()
fillListView()
End Sub

and this one:

Private Sub retBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles retBtn.Click

Select Case comBoxYLevel.SelectedIndex
Case 0
strsql = "select * from boys"
subjListView.items.Clear()
Case 1
strsql = "select * from girls"
subjListView.items.Clear()
End Select

fillListView()
End Sub

---it's just the same... :(

I have this code in my fillListView()

Sub fillListView()
        
            With sqlcmd
                .CommandText = strsql
                .Connection = sqlconn
                End With

            With sqlda
                .SelectCommand = sqlcmd
                .Fill(dTbl)
                End With

        For i = 0 To dTbl.Rows.Count - 1
            With subjListView
                .Items.Add(dTbl.Rows(i)("name"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(dTbl.Rows(i)("age"))
                    End With
                End With
        Next

    End Sub

Try

Sub fillListView()
 
    With sqlcmd
        .CommandText = strsql
        .Connection = sqlconn
    End With
 
    With sqlda
        .SelectCommand = sqlcmd
        .Fill(dTbl)
    End With

    subjListView.Items.Clear()

    For i = 0 To dTbl.Rows.Count - 1
        With subjListView
            .Items.Add(dTbl.Rows(i)("name"))
            With .Items(.Items.Count - 1).SubItems
                .Add(dTbl.Rows(i)("age"))
            End With
        End With
    Next

End Sub

What Jim is saying is correct. You will have to clear the list every time you change the combo box. That way you have a "fresh piece of paper" to load the new data back on.

thanks... but it still the same... I think I don't know where to put the clear method. I did it this way

Select Case comBoxYLevel.SelectedIndex
            Case 0
                subjListView.Items.Clear()
                strsql = "select * from boys"
            Case 1
                subjListView.Items.Clear()
                strsql = "select * from girls"

          
        End Select
        fillListView()

and it's still the same...

oops... wait... I've just read Jim's response... I'll try it

I did what Jim had said but still the same. I just wonder, is there any listview property that affects any clear method?

Member Avatar for Unhnd_Exception

Your filling a global data table. Are you removing the rows before filling it or are you just filling it over and over again. Your not clearing it in your Fill Function. Make sure you clear the rows before filling it with new.

Put a breakpoint at the line with the Clear (line 13 in my previous post) and see if you are actually getting there. Check the contents of the listview at the break then step one line and check again. I tested the subjListView.Items.Clear() line and verified that it does what you want.

It is generally not a good practice to put code in a construct that does not depend on the construct. You have

subjListView.Items.Clear()

in both case clauses therefore it should be moved out of the select. It belongs in fillListView.

thanks Jim for your response. I did what you've said but it still the same. as of now I'm debugging it and studying the possibility of having any list view property affecting any clear method. I'm just new with vb.net and the responses from you and from the others are really great help...

may I know what OS are you using? coz my groupmates were using windows 7 and it these codes were functioning. I'm using windows xp... thanks

I've got it... unhnd exception is right, I was filling a global data table.
I have this line in my module:

Public dTbl As New DataTable

so I should instantiate it everytime I used dTbl. that makes Jim's codes working...
thanks a lot for your responses.a great great help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.