|
-
Apr 3rd, 2007, 09:32 PM
#1
Thread Starter
Member
[2005] Adding a list item in a combo box programmatically
Hi everyone!
Can anyone help how on to add an item in a combox box programmatically in addition to the default values that are retrieved from a certain table in the database.
For example I have a table named "tblDocuments", which lists all the documents related to a particular contract. What I wanted to see in my combo box is an item, let say "<Select>", before it lists all the related documents.
Thanks in advance!
Radic
-
Apr 3rd, 2007, 09:35 PM
#2
Re: [2005] Adding a list item in a combo box programmatically
Firstly, if a ComboBox is bound to a data source then you cannot add items directly. You'd have to add an item to the data source.
Secondly, what you're intending to do is not uncommon but it is pointless. It's like painting the word "Drive" on the side of a car. If you don't want an item selected after you bind the data then set the SelectedIndex property to -1.
-
Apr 3rd, 2007, 09:59 PM
#3
Thread Starter
Member
Re: [2005] Adding a list item in a combo box programmatically
To expound further on the scenario given above. I have ten (10) Questions pertaining to a contract. Each contract may contain one or more related document reference. For example, Contract A has three related documents, namely Document 1, Document 2 & Document 3. In those ten questions, there are some that needs a reference to a particular document, while some dont.
How can i then accommodate those questions that may not need any reference? Because what happens is, since my combobox is bound to a data source, the first item that is shown is Document 1, instead of a temporary item before it.
Please help on how to deal with this. Any suggestions...
Thanks again! I really appreciate your response.
Radic
-
Apr 3rd, 2007, 10:01 PM
#4
Thread Starter
Member
Re: [2005] Adding a list item in a combo box programmatically
To expound further on the scenario given above. I have ten (10) Questions pertaining to a contract. Each contract may contain one or more related document reference. For example, Contract A has three related documents, namely Document 1, Document 2 & Document 3. In those ten questions, there are some that needs a reference to a particular document, while some dont.
How can i then accommodate those questions that may not need any reference? Because what happens is, since my combobox is bound to a data source, in those 10 Questions, the first item that is shown is Document 1, instead of a temporary item before it.
Please help on how to deal with this. Any suggestions...
Thanks again! I really appreciate your response.
Radic
-
Apr 3rd, 2007, 10:37 PM
#5
Re: [2005] Adding a list item in a combo box programmatically
 Originally Posted by radic
what happens is, since my combobox is bound to a data source, the first item that is shown is Document 1, instead of a temporary item before it.
As I said:
 Originally Posted by jmcilhinney
If you don't want an item selected after you bind the data then set the SelectedIndex property to -1.
If some fields are mandatory and some or not then the most appropriate way to show that is with an ErrorProvider. Add one to your form and then for a mandatory ComboBox field you'd handle the SelectedIndexChanged event and do this:
vb Code:
Dim errorText As String = Nothing
If Me.ComboBox1.SelectedIndex = -1 Then
errorText = "You must select a value for this field."
End If
Me.ErrorProvider1.SetError(Me.ComboBox1, errorText)
Now a little icon will appear next to the ComboBox when an item is not selected and it will display that text in a tool tip when the user mouses over it. You can control the properties of that icon in the Properties window for each individual control and the ErrorProvider component itself.
-
Apr 4th, 2007, 02:32 AM
#6
Thread Starter
Member
Re: [2005] Adding a list item in a combo box programmatically
Thanks for the prompt response! =)
If you don't want an item selected after you bind the data then set the SelectedIndex property to -1.
But I found a drawback though on the above statement. What if for example the user have chosen an item then suddenly changed his mind and rather set it to blank. Is there a way to get back to Index -1?
-
Apr 4th, 2007, 02:42 AM
#7
Re: [2005] Adding a list item in a combo box programmatically
 Originally Posted by radic
Is there a way to get back to Index -1?
If you don't want an item selected in a ComboBox then you set the SelectedIndex to -1, plain and simple. If you want the user to be able to clear their selection then you simply handle some appropriate event and do it there. I usually do this:
vb Code:
Private Sub ComboBox1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
'Ctrl+Del or Ctrl+0 clears the selection.
Select Case e.KeyCode
Case Keys.Delete, _
Keys.D0, _
Keys.NumPad0
If e.Control AndAlso _
Not e.Shift AndAlso _
Not e.Alt Then
Me.ComboBox1.SelectedIndex = -1
End If
End Select
End Sub
-
Apr 4th, 2007, 03:50 AM
#8
Thread Starter
Member
Re: [2005] Adding a list item in a combo box programmatically
Nice idea ha! I haven't thought of that...Thanks thanks! =)
-
Apr 4th, 2007, 04:29 AM
#9
Registered User
Re: [2005] Adding a list item in a combo box programmatically
vb Code:
'write this code in form load event
'this code is to load the fields from your database
Dim con As New OleDb.OleDbConnection("type your connection string")
Dim dt As New DataTable
Dim qry As String = "select * from tablename"
Dim da As New OleDb.OleDbDataAdapter(qry, con)
Dim ret As Integer = da.Fill(dt)
ComboBox1.Items.Clear()
If ret > 0 Then
For i As Integer = 0 To ret - 1
ComboBox1.Items.Add(dt.Rows(i).Item("fieldname")
Next
End If
'write this code to add new items in your combobox
ComboBox1.Items.Add("Item1")
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
|