Solved

vb.net - Adding Item To Combo box collection

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

776 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