Solved

vb.net - Adding Item To Combo box collection

Posted on 2007-04-06
3
449 Views
Last Modified: 2008-02-01
I'm trying to add an item  ("<Select Item"> ) to a combo box in the load_gnder sub below.

I've tried "'Me.cbogender.Items.Insert(0, "<Select One>")"  which runs but wont display - and severl other ways to add the item but I cant get it to display in the combo box.


Thanks
[code]
    Private Sub load_gender()
        Dim Dsgender As New DataSet
        Dim Sqlgender As String = ("select Description,indx from gender")
        Dsgender = get_data(Sqlgender)
        Dim dvgen As DataView = Dsgender.Tables(0).DefaultView
        Me.cbogender.DataSource = dvgen
        Me.cbogender.DisplayMember = ("Description")
        Me.cbogender.ValueMember = ("indx")
    End Sub

    Public Function get_data(ByVal Sqlin As String) As DataSet
        Dim Sqldata As String = Sqlin
        Dim conn As New SqlCeConnection(connstr)
        Dim DA1 As SqlCeDataAdapter = New SqlCeDataAdapter(Sqldata, conn)
        Dim ds4 = New DataSet
        DA1.Fill(ds4, "Table1")
        conn.Dispose()
        Return ds4
    End Function

[/code]
0
Comment
Question by:gosonic
3 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 18864052
You can't add items in that fashion to a databound control.  You need to "manually" populate the control and then include your extra item.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 250 total points
ID: 18867941
You could add it to the datatable itself.  Something like - in the load_gender sub

    Dim dr As DataRow = Dsgender.Tables(0).NewRow
    dr("Description") = "<Select One>"
    dr("indx") = 0
    Dsgender.Tables(0).Rows.Add(dr)

But (a) you'd then need to add a Sort to your DataView otherwise it would be the last item in the combo, when I imagine you want it to be the first and (b) it would be a New record (and so added to the database) if you did any .Update on that table.  Or why not put it in your database table?  That would deal with the issue once and for all ;-).

I assume you will have some code for checking whether a user has left it blank in any new record in which it is used, or changed to it in any existing record which s/he is editing.

Roger  
0
 

Author Comment

by:gosonic
ID: 18876388
roger

I really dont want to ad it to the DB table. You solution should work.  
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

11 Experts available now in Live!

Get 1:1 Help Now