Hi,
I have 15 Comboboxes in a form and I have to fill them with the same values from a database table.
If I use the same datatable to fill them all at the same time, everytime I select a a value form a combobox, all the rest show the same value !!!!, I don't know why this happens.
So I tried to use a function to open a new datatable for each Combobox datasource and works fine, but the problem is that my application turns slower when loading that form.
This is my function when all ComboBoxes take the same value when some of them is selected:
Public
Sub FillCombos(ByVal xComboArray As System.Collections.Generic.IList(Of Windows.Forms.ComboBox), ByVal DBConn As SqlClient.SqlConnection) Dim i As Int16 Try Dim ColorTemp As New DataTableColorTemp = ProdQueries("Select * from colores", DBConn).Tables(0)
For i = 0 To 14
xComboArray(i).ValueMember =
"idcolor"xComboArray(i).DisplayMember = "color"
xComboArray(i).DataSource = ColorTemp
Next End Try End Sub
This is my function working OK, but slowing the loading process of the form:
Public
Sub FillCombos(ByVal xComboArray As System.Collections.Generic.IList(Of Windows.Forms.ComboBox), ByVal DBConn As SqlClient.SqlConnection) Dim i As Int16 Try For i = 0 To 14 xCDim ColorTemp As New DataTable
ColorTemp = ProdQueries("Select * from colores", DBConn).Tables(0)
xComboArray(i).ValueMember =
xComboArray(i).DisplayMember = "color"
xComboArray(i).DataSource = ColorTemp
Next End Try End Sub
What would you recommed to achieve this in the most efficient way
Thanks in advance.
George

Fill Several Combobox with the same values from database
Jeff Wasliewicz
Thanks spotty, I know this questions might sound so foolish, but I am new at vb.net
I appreciate your help.
George
Thomas N. Sørensen
This is really simple when you know why. You are binding all the controls to the same datasource and hence they all reference the same item - chnage that item and all of them will chnage. You need to create separate bindingsources and these bindingsources can all reference the same datasource but the controls use the bindingsource as the data source and each control will be independent.
Simple example to demonstrate
The following show in VB 2005 how to achieve what you seeing and how to achieve what you asking for. The difference in behaviour is dependent upon whether a checkbox is checked or not.
Dont worry about the datatable - I create this just so its a self contained sample without the need to access any database.
Youll notice the independent comboboxes have there own bindingsources, whereas the synchronized binds directly with the same dataset.
Public Class Form1
Dim ds As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'//Create a DataTable to use for the binding
ds.Columns.Add("col1")
ds.Columns.Add("col2")
ds.Rows.Add(1, 2)
ds.Rows.Add(3, 4)
ds.Rows.Add(5, 6)
Me.CheckBox1.Checked = True
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
'INDEPENDENT COMBO BOXES
'//Set the dataset as the datasource for two bindingsource
Me.BindingSource1.DataSource = ds
Me.BindingSource2.DataSource = ds
'Bind the comboboxes to the individial BindingSources
Me.ComboBox1.DataSource = Me.BindingSource1
Me.ComboBox1.DisplayMember = "col1"
Me.ComboBox2.DataSource = Me.BindingSource2
Me.ComboBox2.DisplayMember = "col1"
Else
'SYNCHRONIZED COMBO BOXES
'//This Will mean both will bind to same source and hence always
'have the same value
Me.ComboBox1.DataSource = ds
Me.ComboBox2.DataSource = ds
Me.ComboBox1.DisplayMember = "col1"
Me.ComboBox2.DisplayMember = "col1"
End If
End Sub
End Class
eabz
Thanks Spotty, although I'm still using a lot of code trying to fill the Combos with your solution.
I tried to declare an array of BindingSources to reduce the code, but I get "Object reference not set to an instance of an object", and since arrays cannot be declared as "New", I had to fill them one at the time:
Dim Bs(14) As New System.Windows.Forms.BindingSourceDim ColorTemp As DataTable Try
'//Call Function that returns a datatable
ColorTemp = ProdQueries("Select * from colors", DBConn).Tables(0) For i = 0 to 14
Bs(i).DataSource = ColorTemp
xcomboarray(i).ValueMember = "idcolor"
xcomboarray(i).DisplayMember = "color"
xcomboarray(i).DataSource = Bs(i)
Next
Any ideas how to do it with arrays
Wes Payne
Let me clarify what this is doing by chnaging the code a bit
Dim controlname As String = "Combobox" & i + 1.ToString
Dim ctrl As ComboBox = CType(Me.Controls(controlname), ComboBox)
From this you will see I'm constructing a string which will be the name of the control I'm looking for - in this case combobox1,2 ....
I am then using this to reference this control in the forms controls collection and specifically casting it to a type of combobox - so I get intellisense etc. The variable ctrl is a reference variable to the combobox control I'm referring to.
You need to ensure that the controls actually exist and are named correctly so it can find them
Adam Shipp
Spotty,
Just to conclude, your function Dim ctrl As ComboBox = CType(Me.Controls("Combobox" & i + 1.ToString), ComboBox) it returns a Nothing, why is that (I'm using it just like you said)
Thanks in advance
PRMARJORAM
Simple Example
Public Class Form1
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dt.Columns.Add("IDcolor")
dt.Columns.Add("Color")
dt.Rows.Add(1, "Red")
dt.Rows.Add(2, "Blue")
dt.Rows.Add(3, "Green")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Bs(2) As System.Windows.Forms.BindingSource
Dim i As Integer = 0
For i = 0 To 2
Bs(i) = New System.Windows.Forms.BindingSource
Bs(i).DataSource = dt
Dim ctrl As ComboBox = CType(Me.Controls("Combobox" & i + 1.ToString), ComboBox)
ctrl.ValueMember = "idcolor"
ctrl.DisplayMember = "color"
ctrl.DataSource = Bs(i)
Next
End Sub
End Class