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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Based on values in List Box, another list box needs to be shown

Status
Not open for further replies.

nelco

Programmer
Apr 4, 2006
93
US
Hi,

I am new to VB.net and I am trying to create a stand-alone VB.net application. Right now, I am trying to achieve this:

I created a list box. Based on value chosen from the list box, another list box needs to be populated (i have the database set up in sql server)

Any suggestions on how this can be done?
 
Welcome to VB.Net. The ListBox has a SelectedIndexChanged event. This event fires when the selected index changes--meaning when a different item is picked. You can use this event to call some code which looks at your currently selected item, using this as part of your query against the database, or as part of the filter for the DataView which you might use as a datasource for your second list box.
 
Thanks riverguy.

Since I am new, I am wondering how the code should look like?

I started on populating the method for SelectedIndexChanged...but needed some help...

Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex = True Then

        End If

    End Sub
 
Can you post the code which sets the items in your first ListBox?
 
For the first list box, I created a data source in the properties of the list box, and gave the table name from which the list box gets populated, and in the Display Member property, I chose the column name. The code that got populated is as follows:

Code:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DevQADataSet.Core_Attributes' table. You can move, or remove it, as needed.
        Me.Core_AttributesTableAdapter.Fill(Me.DevQADataSet.Core_Attributes)

    End Sub]
 
Ufortunately, you are using a TableAdapter. Anyways, you are setting the DataSource as opposed to scrolling through a DataReader and manually adding Items to the ListBox.

Here are the steps to get you going.

1. Make sure you are filling a DataTable which corresponds to the database table for your second list box.

2. Declare a DataView accessible to the entire class. DataViews are objects that put a filter on a DataTable. Example:
Code:
Dim DV As New DataView

3. After you fill your second DataTable, set the DataView's table equal to that DataTable
Code:
DV.Table = SecondDataTable

4. Set your second ListBox's DataSource to the DataView
Code:
SecondListBox.DataSource = DV

5. When the item selected in the first ListBox changes, alter the RowFilter property of the DataView
Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim CurrentItem As DataRowView = CType(Me.ListBox1.Items(Me.ListBox1.SelectedIndex), DataRowView)
        Me.DV.RowFilter = "SomeColumn = " & CurrentItem.Item("SomeColumnName")
    End Sub

This is just a rough skeleton to get you started. You will have to add error checking and DisplayMember/ValueMember, etc. to your code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top