bsarahim
asked on
categories and subcategories display in the dropdown/gridview
Hi i want to show in the drop drown control all the categories and the sub categeories
based on the table design
SELECT [Id], ,[Name], parentcategoryid
FROM [Category]
Id Name parentcategoryid
1 Books 0
2 Computers 0
3 Desktops 2
4 Notebooks 2
5 Accessories 3
6 Software 2
7 Games 2
8 Electronics 0
9 Camera, photo 8
pls see the image, wht i looking
any help will be appreciated..
dropdown.png
gridview-categories.png
based on the table design
SELECT [Id], ,[Name], parentcategoryid
FROM [Category]
Id Name parentcategoryid
1 Books 0
2 Computers 0
3 Desktops 2
4 Notebooks 2
5 Accessories 3
6 Software 2
7 Games 2
8 Electronics 0
9 Camera, photo 8
pls see the image, wht i looking
any help will be appreciated..
dropdown.png
gridview-categories.png
ASKER
any vb.net code?
here:
Dim parentId As Integer = 0
Dim list As New List(Of String)()
Dim rows = dt.Rows.Cast(Of DataRow)()
For Each item As var In rows
parentId = CInt(item("parentcategoryid"))
Dim name As String = item("Name").ToString()
While parentId > 0
Dim row = rows.Where(Function(n) CInt(n("Id")) = parentId).FirstOrDefault()
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid"))
name = String.Format("{0}->{1}", row("Name"), name)
End If
End While
list.Add(name)
Next
ASKER
im sorry im using Ado.net, asp.net 3.5 not the linq.. thanks
use DataRow.Select method to find the datarow with the right ID:
Dim result As DataRow() = dt.[Select]("Id = " & parentId)
if result.Length>0 Then
Dim row = result[0]
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid"))
name = String.Format("{0}->{1}", row("Name"), name)
End If
End If
ASKER
thanks..
before that, should I read the data in the dataadapater and put back in the table? or wht is the dt here?
before that, should I read the data in the dataadapater and put back in the table? or wht is the dt here?
The dt is the datatable i used it as an example
ASKER
super.. the earlier is code working.. but i have challenges..
For Each item In rows
parentId = CInt(item("parentcategoryi d"))
Dim name As String = item("Name").ToString()
While parentId > 0
Dim row = rows.Where(Function(n) CInt(n("Id")) = parentId).FirstOrDefault()
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid "))
name = String.Format("{0}->{1}", row("Name"), name)
End If
End While
List.Items.Add(name)
Next
I have added the listbox control in the loop
When I change the selectedindexchange..I want to get the parent category Id.
before i insert the value s in to the table... kindly request you to help.. before i award the points
For Each item In rows
parentId = CInt(item("parentcategoryi
Dim name As String = item("Name").ToString()
While parentId > 0
Dim row = rows.Where(Function(n) CInt(n("Id")) = parentId).FirstOrDefault()
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid
name = String.Format("{0}->{1}", row("Name"), name)
End If
End While
List.Items.Add(name)
Next
I have added the listbox control in the loop
When I change the selectedindexchange..I want to get the parent category Id.
before i insert the value s in to the table... kindly request you to help.. before i award the points
to get the parent category id u need to parse the selected item
for instance, if user selected "1->3->8", the parent category id is 3
if user selected "1->2", the parent category id is 1
u need to add the logic where there's no parent category id, when user chooses root category id.
for instance, if user selected "1->3->8", the parent category id is 3
if user selected "1->2", the parent category id is 1
u need to add the logic where there's no parent category id, when user chooses root category id.
Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
Dim curItem As String = listBox1.SelectedItem.ToString()
Dim list As New List(Of Integer)()
Dim tokens =curItem.Split(New String() {"->"}, StringSplitOptions.RemoveEmptyEntries)
For Each item As var In tokens
list.Add(Integer.Parse(item))
Next
if list.Count > 1 then
Dim parentCatId As Integer = list(list.Count - 2)
else
End Sub
ASKER
Thanks..
I have last doubt related to this topic..
1. i want to dispaly the data in the gridview based on the Id, is being fetched on the data row..event
Dim adapter As SqlDataAdapter = New SqlDataAdapter("SELECT [Id] ,[Name] ,[ParentCategoryId] FROM [nopCommerce].[dbo].[Categ ory] where id=" & e.Row.Cells(0).Text, sqlConn)
'im fetching the data of id e.Row.Cells(0).Text
Dim dataSet As DataSet = New DataSet()
adapter.Fill(dataSet, "Ordersvariant")
Dim dt As DataTable = dataSet.Tables(0)
Dim parentId As Integer = 0
' Dim list As New List(Of String)()
Dim rows = dt.Rows.Cast(Of DataRow)()
Dim item As DataRow
For Each item In rows
parentId = CInt(item("parentcategoryi d"))
Dim name As String = item("Name").ToString()
' where(ID = " & e.Row.Cells(0).Text")
While parentId > 0
Dim row = rows.Where(Function(n) CInt(n("id")) = parentId).FirstOrDefault()
' Dim row = rows.Where(Function(n) CInt(e.Row.Cells(0).Text) = parentId).FirstOrDefault()
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid "))
name = String.Format("{0}->{1}", row("Name"), name)
End If
End While
Dim LblShoppingitems As Label
LblShoppingitems = CType(e.Row.FindControl("C ategory"), Label)
LblShoppingitems.Text = name
'List.Items.Add(name)
'List.Items(parentId).Text = name
Next
But this goes in to unended loop and this is not working
2. totally differnt query: I want to display categroy, subcategories,.. in the treeview..
your help is appreciated..
I have last doubt related to this topic..
1. i want to dispaly the data in the gridview based on the Id, is being fetched on the data row..event
Dim adapter As SqlDataAdapter = New SqlDataAdapter("SELECT [Id] ,[Name] ,[ParentCategoryId] FROM [nopCommerce].[dbo].[Categ
'im fetching the data of id e.Row.Cells(0).Text
Dim dataSet As DataSet = New DataSet()
adapter.Fill(dataSet, "Ordersvariant")
Dim dt As DataTable = dataSet.Tables(0)
Dim parentId As Integer = 0
' Dim list As New List(Of String)()
Dim rows = dt.Rows.Cast(Of DataRow)()
Dim item As DataRow
For Each item In rows
parentId = CInt(item("parentcategoryi
Dim name As String = item("Name").ToString()
' where(ID = " & e.Row.Cells(0).Text")
While parentId > 0
Dim row = rows.Where(Function(n) CInt(n("id")) = parentId).FirstOrDefault()
' Dim row = rows.Where(Function(n) CInt(e.Row.Cells(0).Text) = parentId).FirstOrDefault()
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid
name = String.Format("{0}->{1}", row("Name"), name)
End If
End While
Dim LblShoppingitems As Label
LblShoppingitems = CType(e.Row.FindControl("C
LblShoppingitems.Text = name
'List.Items.Add(name)
'List.Items(parentId).Text
Next
But this goes in to unended loop and this is not working
2. totally differnt query: I want to display categroy, subcategories,.. in the treeview..
your help is appreciated..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks...as requested posted in this url
https://www.experts-exchange.com/questions/27658163/categories-and-subcategories-display-in-the-dropdown-gridview-part-2.html
https://www.experts-exchange.com/questions/27658163/categories-and-subcategories-display-in-the-dropdown-gridview-part-2.html
>>But this goes in to unended loop and this is not working
do u mean the For Each loop is infinite?
do u mean the For Each loop is infinite?
ASKER
1. it goes in to unended loop and the webpage goes on requesting ... but never display anything.
2..for your earlier drop down solution im getting the following error..
Private Sub list_SelectedIndexChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChan ged
Dim curItem As String = Listbox1.SelectedItem.Valu e
Dim list As New List(Of Integer)()
Dim tokens = curItem.Split(New String() {"->"}, StringSplitOptions.RemoveE mptyEntrie s)
' Response.Write(tokens.ToSt ring)
'Response.End()
For Each item In tokens
list.Add(Integer.Parse(ite m))
Next
If list.Count > 1 Then
Dim parentCatId As Integer = list(list.Count - 2)
Response.Write(parentCatId )
Else
End If
End Sub
Error details:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 161:
Line 162: For Each item In tokens
Line 163: list.Add(Integer.Parse(ite m))
Line 164: Next
Line 165:
2..for your earlier drop down solution im getting the following error..
Private Sub list_SelectedIndexChanged(
Dim curItem As String = Listbox1.SelectedItem.Valu
Dim list As New List(Of Integer)()
Dim tokens = curItem.Split(New String() {"->"}, StringSplitOptions.RemoveE
' Response.Write(tokens.ToSt
'Response.End()
For Each item In tokens
list.Add(Integer.Parse(ite
Next
If list.Count > 1 Then
Dim parentCatId As Integer = list(list.Count - 2)
Response.Write(parentCatId
Else
End If
End Sub
Error details:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 161:
Line 162: For Each item In tokens
Line 163: list.Add(Integer.Parse(ite
Line 164: Next
Line 165:
dt -> the datatable that contains the data from DB
list -> contains the following list:
Books
Computers
Computers->Desktops
Computers->Notebooks
Computers->Desktops->Acces
Computers->Software
Computers->Games
Electronics
Electronics->Camera, photo
all you gotta do is bind the list to your dropdown list control.
cheers
Open in new window