Binding a combobox to a DataSet

T

Tom

I have a dataset called "employees" with a field
called "gender". How do I bind a combobox to it so that
the user is only allowed to select "M" or "F" and that
value ends up in the dataset?
Note that M and F are not in a table of a database. I
have entered them directly into the Items collection of
the combobox via the property editor.

Thanks
 
C

Cor Ligthert

Hi Tom,
I have a dataset called "employees" with a field
called "gender". How do I bind a combobox to it so that
the user is only allowed to select "M" or "F" and that
value ends up in the dataset?
Note that M and F are not in a table of a database. I
have entered them directly into the Items collection of
the combobox via the property editor.

What is than in the database? While I would not use a combobox for that, for
the user is a checkbox (male) or even better (but some more work) two
radiobuttons

Cor
 
T

Tom

The database has a text field with M or F in it. This is
just one example. I also have a state field for which I
have a combo box that shows the 50 state codes. So I
actually do want to use a combobox. I had used a textbox
in the past and thought this would be a nice improvement.
 
C

Cor Ligthert

Hi Tom,

A little bit crazy sample.

However it works, because I did not use any events I use an extra textbox to
show the behaviour, it needs a project with draged on 3 textboxes, a
combobox and a listbox and this pasted in the code. You can let it go using
the listbox.

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Id")
dt.Columns.Add("Gender")
For i As Integer = 0 To 10
Dim dr1 As DataRow = dt.NewRow
Dim dr2 As DataRow = dt.NewRow
dr1(1) = "M"
dr2(1) = "F"
dr1(0) = i.ToString
dr2(0) = i.ToString & i.ToString
dt.Rows.Add(dr1)
dt.Rows.Add(dr2)
Next
Dim dc As New DataTable
dc.Columns.Add("M/F")
dc.Columns.Add("Gender")
Dim dra As DataRow = dc.NewRow
Dim drb As DataRow = dc.NewRow
dra(1) = "M"
drb(1) = "F"
dra(0) = "Male"
drb(0) = "Female"
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
dc.Rows.Add(dra)
dc.Rows.Add(drb)
Me.TextBox1.DataBindings.Add("text", dt, "Id")
Me.ComboBox1.DataBindings.Add("SelectedValue", dt, "Gender")
Me.ComboBox1.DataSource = dc
Me.ComboBox1.ValueMember = "Gender"
Me.ComboBox1.DisplayMember = "M/F"
Me.TextBox2.DataBindings.Add("text", ComboBox1, "SelectedValue")
Me.TextBox3.DataBindings.Add("text", dt, "Gender")
Me.ListBox1.DataSource = dt
Me.ListBox1.DisplayMember = "Id"
End Sub
///
 
G

Guest

I tried your code and it worked, but it wasn't exactly
what I was looking for. I had created the combobox almost
completely through the visual interface and had used the
Items property to save a list of the possible values. I
had then bound it to my dataset by binding the text
property. This did not properly save the selected value
to the dataset. It turns out it worked properly when
instead I bound the selecteItem property to the dataset.
Why that is, I don't know, but it works.

Thanks for the help. Your example (and a bit more
reading) helped me understand databinding a little better.
Tom
 

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