Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 listboxes one problem

Status
Not open for further replies.

Silo4

Programmer
Nov 7, 2002
17
GB
Hi,

I have 2 listboxes on a windows form (lstStatements & lstChoices). I databind the first list from a table dataset and then when I change the selection in this list i databind the second list from a second table in the dataset which is filtered by the selection in the first list. My problem is that as soon as the selection is made in the first list, the items in that list no longer read as they should but each line changes to "System.Data.DataRowView". Here's the code....

Code:
Private Sub lstStatements_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstStatements.SelectedIndexChanged
        GetChoicesProc()
        FillChoicesProc()
End Sub

Private Sub GetChoicesProc()
        Data1.GetDataBase("SELECT * FROM tChoices WHERE StatementID = " & lstStatements.SelectedValue.ToString & " ORDER BY ShowOrder", "tChoices")
End Sub

Private Sub FillChoicesProc()
        lstChoices.DataSource = Data1.DataSet.Tables("tChoices")
        lstChoices.DisplayMember = "Choice"
        lstChoices.ValueMember = "ChoiceID"
End Sub

It seems to work fine until i get to the line...

Code:
lstChoices.ValueMember = "ChoiceID"

Any help here would be greatly appreciated.

Thanks
 
A more efficient way to do this is to use a DataView as the source for lstChoices and set the DataView's RowFilter property when the selection in lstStatements changes. Here's some code:

'declare this globally
Dim dvChoices As DataView

'fill the dataset
Data1.GetDataBase("SELECT * FROM tChoices ORDER BY ShowOrder", "tChoices"

'put this where you first get the data from the Choices table, after filling the dataset
dvChoices = Data1.DataSet.Tables("tChoices").DefaultView
dvChoices.RowFilter = "StatementID=-9999999" 'dummy value to ensure no records show when nothing is selected in lstChoices

'put this where you first bind the data to lstChoices
lstChoices.DataSource = dvChoices
lstChoices.DisplayMember = "Choice"
lstChoices.ValueMember = "ChoiceID"

Private Sub lstStatements_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstStatements.SelectedIndexChanged
If lstStatements.SelectedIndex > -1 Then
dvChoices.RowFilter = "StatementID=" & lstStatements.SelectedValue.ToString
Else
dvChoices.RowFilter = "StatementID=-9999999" 'dummy value to ensure no records show when nothing is selected in lstChoices
End If
End Sub


This way you don't have to keep going back to the database every time you click on lstStatements. All of the Choices data are already there, you just filter what is displayed based upon the selection in lstChoices.

Post again if you have any questions.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Excellent, thanks very much for your help jebenson. After a little tweaking it works like a dream!

Ah shall well be preparin' m'self fer talkin' like a massively bearded pirate! Arrr!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top