By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,263 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,263 IT Pros & Developers. It's quick & easy.

Fill data toCombo box from dataset

P: n/a
Hello all,

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

regards

lal
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

"K R Lal" <la**@yahoo.com> schreef in bericht
news:eY**************@TK2MSFTNGP14.phx.gbl...
Hello all,

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

regards

lal

Nov 21 '05 #2

P: n/a

"K R Lal" <la**@yahoo.com> wrote in message
news:eY**************@TK2MSFTNGP14.phx.gbl...
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
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #3

P: n/a
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

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:#C**************@TK2MSFTNGP09.phx.gbl...

"K R Lal" <la**@yahoo.com> wrote in message
news:eY**************@TK2MSFTNGP14.phx.gbl...
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
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 21 '05 #4

P: n/a

"K R Lal" <la**@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.