Hi..i'm using VB .NET. I have create a fairly simple form which has bound a combo box to a value in a datatable as such
Me.cbContact.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.dscustomer, "Contacts.Contactname"))
Me.cbContact.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.dscustomer, "Contacts.ContactID"))
Me.cbContact.DataSource = dscustomer.Tables("Contacts")
Me.cbContact.DisplayMember = "ContactName"
Me.cbContact.ValueMember = "ContactID"
What's confusing me is that when i add an entry with 1 name i then hit the create button on the form and the name in the combo box disappears. I then create another name and after hitting the dropdown button on the combo box the 2nd name i entered appears twice. If i then add a 3rd name and hit the create button, then click on the dropdown list, i get the 3rd name showing first, then the 2nd name i entered and then the 3rd name again.
The interesting part is that when i committ the changes to the database it does so correctly, that is with the 3 different names as 3 different contacts.
From doing some of my own debugging on this, it seems when i use the DataRow.NewRow function it always adds the row at index 0. But the first name i entered has been added to the table at index 0 as its the first record to be added. Therefore each time i'm adding a row its adding it at position 0 but not actually overwritting the information at row 0 (ie: the first name i have entered)......hope the explanation is not too confusing...any help would be greatly appreciated...
Just as an update to this, I have tried to use the DataRow.InsertAt() method to insert the first row at position 0, then the next at position 1 etc etc, however it still seems that the row is being added at position 0.
newrow("CustomerID") = tbCustomer.Tag
newrow("ContactName") = cbContact.Text
newrow("PhoneNumber") = tbPhone.Text
newrow("FaxNumber") = tbFax.Text
where insertrow is incremented by 1 each time a row is added to the datarow. I'm still not clear as to why the combobox is not showing the 3 entries.
OK...I have reproduction steps for this if it will help ....
Prerequesits - connection to a database with 1 table called "Contacts".
Now create a form and using the sqldataadapter wizard (i'm connecting to a msde database) create a dataadapter with a simple "SELECT * FROM CONTACTS" as the command.
Now go to Data -> Generate DataSet and make sure you add it to the designer.
Add a combobox and button to the form.
In the designer bind the combobox.selectedtext -> dataset.contacts.contactname
combobox.selectedvalue -> dataset.contacts.contactID (unique identifier and autoincrementer for the table)
combobox.datasource -> dataset.contacts
combobox.displaymember -> contactname
combobox.displayvalue -> contactID
Double click on the form to take you to the code for the events form_load. This is the code i have for the form:
Public Class Form1
#Region " Windows Form Designer generated code "
Dim newrow As DataRow
Dim addat As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
addat = 0
newrow = Cbdata1.Contacts.NewRow
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
newrow("ContactName") = ComboBox1.Text
newrow("CustomerID") = 1
addat += 1
newrow = Cbdata1.Contacts.NewRow
If you know run this in debug, click in the combo box and enter Name1 then click the submit button. Now click in the combobox and add Name2 and click on the button.
Now click the dropdown arrow on the combobox and you should see Name2 showing twice. Now add a 3rd name, Name3. Click the submit button. Now click on the dropdown arrow again and you should see Name3 then name2 then name3. Why is this??? If you then updated the database with this dataset you will find that Name1, Name2 and Name3 get added so that is ok, but how come originally it seems as though Name1 disappears??