-
Feb 17th, 2013, 03:22 AM
#1
Thread Starter
Member
subitems in combobox
Hi,
I have a database table as expense with columns master and submaster.
Now i need to show this values in combobox. if the value in master is a submaster of any master then it should be added as sub item of that...I know no one understood this...so below is an example
Database:
Master Submaster
------- -----------
Rent Home Rent
Etisalat Abu Dhabi
Rent Office Rent
Etisalat Dubai
Office Rent Car Rent
Abu Dhabi Mobile
Abu Dhabi Landline
Now in combobox it should display as:
Rent
>Home Rent
>Office Rent
>Car Rent
Etisalat
>Abu Dhabi
>Mobile
>Landline
>Dubai
I hope I have cleared my problem...Please help if anyone can
Thanks in advance
-
Feb 17th, 2013, 03:59 AM
#2
Re: subitems in combobox
There's no such thing as subitems in a ComboBox. If you want the list to look like a tree then you can either create a custom ComboBox control that actually displays a TreeView in its drop-down list or else simply add a prefix to the text of each "subitem" in the list, e.g. a checvron as you have done in your post or some spaces.
-
Feb 17th, 2013, 04:07 AM
#3
Thread Starter
Member
Re: subitems in combobox
Originally Posted by jmcilhinney
There's no such thing as subitems in a ComboBox. If you want the list to look like a tree then you can either create a custom ComboBox control that actually displays a TreeView in its drop-down list or else simply add a prefix to the text of each "subitem" in the list, e.g. a checvron as you have done in your post or some spaces.
Thanks for your reply, but Im a newbie...can you give some code for this example..so that i can understand...The problem is not only with displaying but how I will know its subitem and the order it is present
-
Feb 17th, 2013, 04:11 AM
#4
Thread Starter
Member
Re: subitems in combobox
If its listbox also no problem...plz give code to do this....
-
Feb 17th, 2013, 08:31 AM
#5
Thread Starter
Member
-
Feb 17th, 2013, 10:39 AM
#6
Re: subitems in combobox
Why not simply use a TreeView for this data instead of a ComboBox ? Your data is hierarchical in nature and a TreeView lends itself perfectly for this.
-
Feb 18th, 2013, 01:20 AM
#7
Thread Starter
Member
Re: subitems in combobox
Originally Posted by Niya
Why not simply use a TreeView for this data instead of a ComboBox ? Your data is hierarchical in nature and a TreeView lends itself perfectly for this.
The problem is any no.of subitems can be added dynamic time...I have given you ex in my 1st post..can you help with some code to show the above data in treeview...im not able to code it...fully messed up...please help
-
Feb 19th, 2013, 02:14 AM
#8
Thread Starter
Member
Re: subitems in combobox
Originally Posted by jmcilhinney
There's no such thing as subitems in a ComboBox. If you want the list to look like a tree then you can either create a custom ComboBox control that actually displays a TreeView in its drop-down list or else simply add a prefix to the text of each "subitem" in the list, e.g. a checvron as you have done in your post or some spaces.
Private Sub FillCombobox(ByRef cmb As DataGridViewComboBoxColumn)
Dim a(100) As String
Dim b(100) As String
Dim m As Integer = 0 'Count of main masters i.e items not having masters
Dim cmd As SqlCommand = New SqlCommand("SELECT count(DISTINCT item) from expense", cl.sqlConn())
Dim total As Integer = cmd.ExecuteScalar()
cmd = New SqlCommand("SELECT DISTINCT item from expense where master=''", cl.sqlConn())
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows() Then
While (dr.Read())
cmb.Items.Add((dr.Item(0)).ToString())
a(m) = (dr.Item(0)).ToString()
b(m) = (dr.Item(0)).ToString()
m = m + 1
End While
End If
dr.Close()
Dim index As Integer = 0
Dim count As Integer
Dim s As String
Dim p As Integer
Dim ss As String = ""
Dim ss1 As String = ""
Dim cc As Integer
Dim level As Integer = 1
While True
For i As Integer = 0 To m - 1 Step 1
cmd = New SqlCommand("SELECT DISTINCT item from expense where master='" + a(i) + "' order by item desc", cl.sqlConn())
dr = cmd.ExecuteReader()
If dr.HasRows() Then
index = cmb.Items.IndexOf(b(i))
cc = b(i).Split("*").Length - 1
ss = ""
ss1 = ""
For g As Integer = 0 To cc Step 1
ss = ss + "*"
ss1 = ss1 + " "
Next
While (dr.Read())
cmb.Items.Insert(index + 1, ss1 + ss + dr(0).ToString())
cmb.ValueMember = dr(0).ToString()
End While
End If
Next
'FillArray(a(100), level) Fill only those items in array which are in that level
m = 0
For i As Integer = 0 To cmb.Items.Count - 1 Step 1
s = cmb.Items(i).ToString()
count = s.Split("*").Length - 1
If count >= level Then
p = s.IndexOf("*")
a(m) = s.Substring(p + level)
b(m) = s
m = m + 1
End If
Next
'FillArray(a(100), level)
level = level + 1 'incrementing level
If cmb.Items.Count = total Then
Exit While
End If
End While
End Sub
I wrote above code and it gives me rsult i wanted but the problem is on selecting an item it shows **Home Rent ...I want to show juz Home Rent...Iam able to remove star and get the new string but combobox displays the new string and then becomes empty...this combobox is in datagridview...
is there a way to add display member and value member for combobox in datagridview...as its not coming from dataset...im calculating and inserting each row..so how to add different text for itemvalue..?
-
Feb 19th, 2013, 02:57 AM
#9
Re: subitems in combobox
Well it means that there is a logical error somewhere in your code. Single step through that code and check the values of the variables to make sure they're what you expect them to be and that the logic is doing what you're expecting it to do.
-
Feb 19th, 2013, 03:06 AM
#10
Thread Starter
Member
Re: subitems in combobox
Originally Posted by Niya
Well it means that there is a logical error somewhere in your code. Single step through that code and check the values of the variables to make sure they're what you expect them to be and that the logic is doing what you're expecting it to do.
Hi nivya,
you didnt get my question there is no logical error its working as i wanted ,,,if i select **home rent it dispalys that only...but for user readability i want it to be home rent...i mean i want to format the selected item and dispaly..but i cant do that in selectedindex changed
-
Feb 19th, 2013, 04:01 AM
#11
Thread Starter
Member
Re: subitems in combobox
cmb.Items.Insert("abc")
This inserts abc in cmb.with insert only can i have different displayed text and value ? I hope now my doubt is simple
-
May 5th, 2013, 08:08 AM
#12
Thread Starter
Member
Re: subitems in combobox
If someone needs solution for this :
I used Bradely combo treebox
http://www.brad-smith.info/blog/archives/193
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|