473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Binding a combobox to a DataSet

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
Nov 20 '05 #1
4 3748
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
Nov 20 '05 #2
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.
-----Original Message-----
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, forthe user is a checkbox (male) or even better (but some more work) tworadiobuttons

Cor
.

Nov 20 '05 #3
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
///
Nov 20 '05 #4
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
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times...
5
by: Greg Dunn | last post by:
I have two tables in a parent-child relationship. Details on the tables are as shown below: Table1 ------- Item_ID (primary key) Item_Owner (foreign key to Table2) Table2 -------
13
by: Paul Slavin | last post by:
I have a textbox bound to a dataview, when I update the text in the textbox no changes take place in the underlying dataset. Why is this?? any answers appreciated, as to due to the underlying...
1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
10
by: Richard | last post by:
I have created a form which sets up a dataview. The form views one record at a time using a currencymanager. This works fine. All my text boxes bind. However I have a combo box which gets its...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
0
by: oracle | last post by:
Greetings, I have a combo box that I binded to a data set using text and tags. I want it to display the DRT.Name property and have a DRT.UnitId as a value. ...
3
by: Peter | last post by:
OK, so in addition to the problem I mentioned before (c.f. "Data Binding to Textbox"), I realized that my comboboxes are not really bound to the internal data set. They are just set to manually...
1
by: Monty M. | last post by:
Does anyone know how to perform two way data binding between a combo box and a listview. The listview is bound to a dataset table in code: Binding Bind = new Binding(); DataTable dt;...
10
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi, Problem: How can I databind (or put) a SqlServer query's row return of 115,000 items into a ComboBox quickly? Not much longer than a matter of seconds, that is... Scenario: I am...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.