ListBox problem

i have 2 table
table1 : categoryID,CategoryName,Desc
table2: subCategoryID,SubCateogryName,subCategoryDesc,CategoryID
link by CategoryID

i have 2 listbox
listbox1 : display all categoryName
listbox2: display subCategoryName base on categoryName selected at the listbox1

my code :

Private ds As DataSet = New DataSet

Public Sub getCategory(ByVal ds As DataSet)
        Dim strSql As String = "select * from Category"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(strSql, DBConn)
        da.Fill(ds, "Category")

    End Sub
    Public Sub getSubCategory(ByVal ds As DataSet)
        Dim strSql As String = "select * from SubCategory"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(strSql, DBConn)
        da.Fill(ds, "SubCategory")
    End Sub

       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getCategory(ds)
        getSubCategory(ds)
        ListBox1.DataSource = ds.Tables("category")
            ListBox1.DisplayMember = ds.Tables("category").Columns(1).ToString
            ListBox1.ValueMember = ds.Tables("category").Columns(0).ToString
    End Sub


any can help me ?
jokerxp888Asked:
Who is Participating?
 
ptakjaConnect With a Mentor Commented:
The problem is on the line:

ListBox2.DataSource = ds.Tables("subCategory").Select("cat_id=1")

The Select method of the Tables object returns an Array of DataRow objects. So you have a couple options:

1) Take the array of DataRow objects and loop through them adding them to the Listbox sort of manually.
2) Add the array of DataRow objects to another DataTable and then bind the listbox to the new data table.

0
 
ptakjaCommented:
What's the problem?
0
 
jokerxp888Author Commented:
i want to make it
listbox1 : display all categoryName
listbox2: display subCategoryName base on categoryName selected at the listbox1


0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jokerxp888Author Commented:
i have error at

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            ListBox2.DataSource = ds.Tables("subCategory").Select("cat_id=1")
            ListBox2.DisplayMember = ds.Tables("subCategory").Columns(1).ToString
            ListBox2.ValueMember = ds.Tables("subCategory").Columns(0).ToString
        Catch err As Exception
            MsgBox(err.ToString)
        End Try
    End Sub
0
 
wguerramCommented:
What's the error you get?
0
 
wguerramCommented:
ptakja took a good point.

You can use the default view of the datatable

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            dim dv as Data.DataView = ds.Tables("subCategory").DefaultView

            dv.Sort = "cat_id"
            dv.RowFilter = "1"

            ListBox2.DataSource = dv
            ListBox2.DisplayMember = ds.Tables("subCategory").Columns(1).ToString
            ListBox2.ValueMember = ds.Tables("subCategory").Columns(0).ToString
        Catch err As Exception
            MsgBox(err.ToString)
        End Try
    End Sub
0
 
ptakjaCommented:
I would say split between ptakja & wguerram.
0
All Courses

From novice to tech pro — start learning today.