Solved

vb.net - Adding Item To Combo box collection

Posted on 2007-04-06
3
452 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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