Results 1 to 12 of 12

Thread: subitems in combobox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    Re: subitems in combobox

    Quote Originally Posted by jmcilhinney View Post
    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

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    Re: subitems in combobox

    If its listbox also no problem...plz give code to do this....

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    Re: subitems in combobox

    Please someone help

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,601

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    Re: subitems in combobox

    Quote Originally Posted by Niya View Post
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    Re: subitems in combobox

    Quote Originally Posted by jmcilhinney View Post
    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..?

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,601

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    Re: subitems in combobox

    Quote Originally Posted by Niya View Post
    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

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    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

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    37

    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
  •  



Click Here to Expand Forum to Full Width