Solved

ListBox problem

Posted on 2004-10-31
226 Views
Last Modified: 2012-05-05
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 ?
0
Question by:jokerxp888
    7 Comments
     
    LVL 14

    Expert Comment

    by:ptakja
    What's the problem?
    0
     

    Author Comment

    by:jokerxp888
    i want to make it
    listbox1 : display all categoryName
    listbox2: display subCategoryName base on categoryName selected at the listbox1


    0
     

    Author Comment

    by:jokerxp888
    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
     
    LVL 8

    Expert Comment

    by:wguerram
    What's the error you get?
    0
     
    LVL 14

    Accepted Solution

    by:
    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
     
    LVL 8

    Expert Comment

    by:wguerram
    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
     
    LVL 14

    Expert Comment

    by:ptakja
    I would say split between ptakja & wguerram.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now