Fill data toCombo box from dataset

K

K R Lal

Hello all,

how can i fill the data to combo box from dataset
please help me.

regards

lal
 
P

Peter Proost

Hi this bit of code shows you how to fill a combo with a sqldatareader (to
do it with a dataset is almost the same)and if you want you can also set it
to resize the dropdown part so that everything is fully displayed.

'Vul Combobox
Dim cmdReader As SqlCommand
Dim rdrReader As SqlDataReader
Dim cnComboBox As New SqlConnection("Connectionstring")
Dim blnDropDownMax As Boolean

cboCombo.Items.Clear()
cnComboBox.Open()
cmdReader = New SqlCommand("Your select statemen", cnComboBox)
rdrReader = cmdReader.ExecuteReader

Do While rdrReader.Read
If rdrReader("field") Is DBNull.Value Then
cboCombo.Items.Add("")
Else
cboCombo.Items.Add(directcast(rdrReader("field"),
String))
End If

Loop

cnComboBox.Close()

'toevoeging door Peter 16/12/2004 voor het automatisch
groter maken van de
'dropdown zodat deze even groot is als het grootste item

'Resize of the dropdownpart
If blnDropDownMax = True Then
Dim ds As Graphics = cboCombo.CreateGraphics
Dim x As Single = 0
Dim str As String
For Each str In cboCombo.Items
x = Math.Max(x, ds.MeasureString(str,
cboCombo.Font).Width)
Next
cboCombo.DropDownWidth = CInt(x) + 7
ds.Dispose()
End If

End If
End Sub

Be aware there's no error handling, you have to add that yourself. It's just
an example to get you started

hth Peter
 
P

Peter van der Goes

K R Lal said:
Hello all,

how can i fill the data to combo box from dataset
please help me.

regards

lal
Try something like this, once your dataset has been populated:

With <your combo box>

.DataSource = <your dataset>

.DisplayMember = "field to display"

.ValueMember = "field to use as value member"

.SelectedIndex = -1 'to have the combo box appear empty when container
form first displays

End With
 
K

K R Lal

Dear Mr. Peter,
I tryied, but i can't understand what is the valumember, and I need one help
also. On data binding I want to change the "bindingcontext" on the combo
selected_index event.

please help me.

Lal
 
P

Peter van der Goes

K R Lal said:
Dear Mr. Peter,
I tryied, but i can't understand what is the valumember, and I need one
help
also. On data binding I want to change the "bindingcontext" on the combo
selected_index event.

please help me.

Lal
Please read the following articles available in the MSDN help (.NET
Framework filter):
ComboBox class
ComboBox members
ListControl.ValueMember property

The articles include code examples and should clear up any questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top