combobox datasource

ricio2000

Member
Joined
May 2, 2005
Messages
18
Location
Hialeah, FL
Programming Experience
1-3
Hi everybody, I had an issue where I have a combobox that gets populated from a DataSet.

Dim dataTableObj As DataTable = customerDataSet.item("customer")
cbCompany.DataSource = dataTableObj
I have another comboBox named
companyType
I'm populating the cbCompany comboBox based on the value of the companyType, so I do this in the selectedIndex event. I keep getting the values repeated over and over again into the items collection of the cbCompany comboBox. I can't call the clear method of the cbCompany because it will give me a run time error indicating that i can't clear the comboBox when the DataSource is set. I tried setting the dataSource to null and then calling the clear method, but nothing will clear in the cbCompany comboBox. Can somebody help me with this ? Gracias
 
If you want to specify the items in the drop down list you shouldn't be using a data source. You could bind the Text property if you want the selected item to update a value elsewhere, but you should populate and clear the item list manually.
 
huh?

Well, what I'm doing is the following I have these two combo boxes. And I have this DataSet that I'm filling with two tables, one table contains the column customerType, the other table contains the customernames and customertypes, but the second table was a query that did a select based on where the customertype = selectedindex from the combobox called cbCustomerType. That I have binded to a datatable as follows

Private Sub populateCustomerType()
Dim customerTable As DataTable = _ customerDataSet.Tables("customertype")
Me.cbCustomerType.DataSource = customerTable
Me.cbCustomerType.DisplayMember = "customertype"
End Sub

Private Sub populateCustomer()
Dim customerTable As DataTable = customerDataSet.Tables("customer")
Me.cbCompany.DataSource = customerTable
Me.cbCompany.DisplayMember = "customerName"
End Sub

This is my selectedindex changed event:

Private Sub cbCustomerType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCustomerType.SelectedIndexChanged
Me.cbCompany.DataBindings.Clear()
Dim myDRW As DataRowView
Dim custype As String
myDRW = CType(cbCustomerType.SelectedItem, DataRowView)
custype = CStr(myDRW.Row.Item("customertype")).Trim()
currentCustomer.fillCustomer(custype)
Me.populateCustomer()

End Sub

I call the first method, (populateCustomerType) in the in the load event and then I called the populateCustomer in selectedIndexChanged event of the cbCustomerType_SelectedIndexChanged event. So what you are telling is not to bind the datasource of the second comboBox(cbCompany) , but to add them the regular way. That sort of defeats the purpose of having the DataSource property. Any other idea's of suggestions.
 
I guess this can be closed. I figured it out. The problem is that I wasn't clearing the customerDataSet.Tables("customer")... so when I called the fill method. It was adding duplicate rows. I fixed this by checking that the customerDataSet.Tables("customer") was not nothing and that customerDataSet itself was not nothing so that I can call the clear method on that table. This fixed my dilema. Wow, what a mission. I dind't know the fill method added rows. I thought it overwride them. Boy was I wrong. Gracias.
 
Back
Top