jokerxp888
asked on
ListBox problem
i have 2 table
table1 : categoryID,CategoryName,De sc
table2: subCategoryID,SubCateogryN ame,subCat egoryDesc, 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").Colu mns(1).ToS tring
ListBox1.ValueMember = ds.Tables("category").Colu mns(0).ToS tring
End Sub
any can help me ?
table1 : categoryID,CategoryName,De
table2: subCategoryID,SubCateogryN
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").Colu
ListBox1.ValueMember = ds.Tables("category").Colu
End Sub
any can help me ?
What's the problem?
ASKER
i want to make it
listbox1 : display all categoryName
listbox2: display subCategoryName base on categoryName selected at the listbox1
listbox1 : display all categoryName
listbox2: display subCategoryName base on categoryName selected at the listbox1
ASKER
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").S elect("cat _id=1")
ListBox2.DisplayMember = ds.Tables("subCategory").C olumns(1). ToString
ListBox2.ValueMember = ds.Tables("subCategory").C olumns(0). ToString
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
ListBox2.DataSource = ds.Tables("subCategory").S
ListBox2.DisplayMember = ds.Tables("subCategory").C
ListBox2.ValueMember = ds.Tables("subCategory").C
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
What's the error you get?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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").D efaultView
dv.Sort = "cat_id"
dv.RowFilter = "1"
ListBox2.DataSource = dv
ListBox2.DisplayMember = ds.Tables("subCategory").C olumns(1). ToString
ListBox2.ValueMember = ds.Tables("subCategory").C olumns(0). ToString
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
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").D
dv.Sort = "cat_id"
dv.RowFilter = "1"
ListBox2.DataSource = dv
ListBox2.DisplayMember = ds.Tables("subCategory").C
ListBox2.ValueMember = ds.Tables("subCategory").C
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
I would say split between ptakja & wguerram.