Combo Boxes and Data Binding in VB.NET


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Combo Boxes and Data Binding in VB.NET

  1. #1
    Stephen Lee Guest

    Combo Boxes and Data Binding in VB.NET


    Hi guys

    I've run into a problem with data binding a combo box:

    I have two data tables, one containing a field that is a foreign key to a
    "Lookup"-style table.

    The second datatable is the lookup table itself.

    What I would like to do is bind the text of the combo box to the first table's
    "Foreign Key" field, and the combo's "Items" to the second datatable. The
    effect would be that the user sees all possible field values (from the lokoup
    table) when they dropdown the combo.

    Having read the VB.NET documentation, I am none the wiser as to whether this
    is possible or not. Has anyone done this using data binding in VB.NET, or
    am I better off rolling my own?

    Thanks in advance,
    Stephen Lee
    ComputerCORP.

  2. #2
    Kathleen Dollard Guest

    Re: Combo Boxes and Data Binding in VB.NET

    Stephen,

    > I've run into a problem with data binding a combo box:
    >
    > I have two data tables, one containing a field that is a foreign key to a
    > "Lookup"-style table.
    >
    > The second datatable is the lookup table itself.
    >
    > What I would like to do is bind the text of the combo box to the first

    table's
    > "Foreign Key" field, and the combo's "Items" to the second datatable. The
    > effect would be that the user sees all possible field values (from the

    lokoup
    > table) when they dropdown the combo.


    If I understand you this is fairly common use of a bound combo box. Set the
    DataSource to your lookup table and the DisplayMember to what you would like
    displayed. you can also stuff your primary key in the ValueMember.
    (CustOrderExt and CountryLookupExt are datasets)

    Me.cboPos.DataSource = Me.CustOrderExt
    Me.cboPos.DisplayMember = "Customers.CompanyName"
    Me.cboPos.ValueMember = "Customers.CustomerID"

    Me.cboCountry.DataSource = Me.CountryLookupExt.CountryLookup
    Me.cboCountry.DisplayMember = "Country"
    Me.cboCountry.ValueMember = "Country"

    Then the set the databinding. If you bind to the same currency manager, the
    two datasets will be linked. In the above, the first combo box is a
    navigational tool on a toolbar, so its binding is set to syncrhonize the
    remaining elements on the form. The second is the standard combo box
    behavior. Databindings for both are shown below.

    AddBinding(cboPos, "SelectedValue", Me.CustOrderExt,
    "Orders.CustomerID")
    AddBinding(cboCountry, "SelectedValue", Me.CustOrderExt,
    "Customers.Country")

    Note that the first binding is to the same datasource as the combo box,
    while the second is to a different datasource.

    >
    > Having read the VB.NET documentation, I am none the wiser as to whether

    this
    > is possible or not. Has anyone done this using data binding in VB.NET, or
    > am I better off rolling my own?


    IMO the VB.NET tools are great.

    Kathleen



  3. #3
    Guest

    Re: Combo Boxes and Data Binding in VB.NET


    Hi Kathleen

    Thanks! This is excellent. Only two issues:

    1.) With one such combo on a dataform, every record I navigate to, not including
    the first record, is marked as "edited", even when I have simply navigate
    onto the rcord then immediately navigated off to another record. What I mean
    to say here is that doing a GetChanges() on the dataset reveals that this
    record as being marked as "edited". This seems wrong. The record should
    not be marked as such until I actually make changes.

    also

    2.) If I do edit the record by selecting another value from the combo, once
    I've selected the new value, and the drop down list has disappeared, I get
    a blank combo text value. Once focus moves to a nother control, the newly
    selected value is displayed, and the underlying datarow takes the newly-selected
    value.

    Have you come across either of these situations?

    Thanks again for your time!

    Stephen

    "Kathleen Dollard" <kathleen@mvps.org> wrote:
    >Stephen,
    >
    >> I've run into a problem with data binding a combo box:
    >>
    >> I have two data tables, one containing a field that is a foreign key to

    a
    >> "Lookup"-style table.
    >>
    >> The second datatable is the lookup table itself.
    >>
    >> What I would like to do is bind the text of the combo box to the first

    >table's
    >> "Foreign Key" field, and the combo's "Items" to the second datatable.

    The
    >> effect would be that the user sees all possible field values (from the

    >lokoup
    >> table) when they dropdown the combo.

    >
    >If I understand you this is fairly common use of a bound combo box. Set

    the
    >DataSource to your lookup table and the DisplayMember to what you would

    like
    >displayed. you can also stuff your primary key in the ValueMember.
    >(CustOrderExt and CountryLookupExt are datasets)
    >
    > Me.cboPos.DataSource = Me.CustOrderExt
    > Me.cboPos.DisplayMember = "Customers.CompanyName"
    > Me.cboPos.ValueMember = "Customers.CustomerID"
    >
    > Me.cboCountry.DataSource = Me.CountryLookupExt.CountryLookup
    > Me.cboCountry.DisplayMember = "Country"
    > Me.cboCountry.ValueMember = "Country"
    >
    >Then the set the databinding. If you bind to the same currency manager,

    the
    >two datasets will be linked. In the above, the first combo box is a
    >navigational tool on a toolbar, so its binding is set to syncrhonize the
    >remaining elements on the form. The second is the standard combo box
    >behavior. Databindings for both are shown below.
    >
    > AddBinding(cboPos, "SelectedValue", Me.CustOrderExt,
    >"Orders.CustomerID")
    > AddBinding(cboCountry, "SelectedValue", Me.CustOrderExt,
    >"Customers.Country")
    >
    >Note that the first binding is to the same datasource as the combo box,
    >while the second is to a different datasource.
    >
    >>
    >> Having read the VB.NET documentation, I am none the wiser as to whether

    >this
    >> is possible or not. Has anyone done this using data binding in VB.NET,

    or
    >> am I better off rolling my own?

    >
    >IMO the VB.NET tools are great.
    >
    >Kathleen
    >
    >



  4. #4
    Kathleen Dollard Guest

    Re: Combo Boxes and Data Binding in VB.NET

    Stephen,

    > 1.) With one such combo on a dataform, every record I navigate to, not

    including
    > the first record, is marked as "edited", even when I have simply navigate
    > onto the rcord then immediately navigated off to another record. What I

    mean
    > to say here is that doing a GetChanges() on the dataset reveals that this
    > record as being marked as "edited". This seems wrong. The record should
    > not be marked as such until I actually make changes.


    Can you first carefully check the case of all properties (such as "Text" vs
    "text"). There is a known bug there. If it continues to appear edited, can
    you post your code and I will look at it.

    > 2.) If I do edit the record by selecting another value from the combo,

    once
    > I've selected the new value, and the drop down list has disappeared, I get
    > a blank combo text value. Once focus moves to a nother control, the newly
    > selected value is displayed, and the underlying datarow takes the

    newly-selected
    > value.


    I am sorry, I didn't follow this. One possiblity is the need for the
    EndCurrentEdit in the Validation event, but as I don't understand your
    scenario here, I don't know if that is the solution

    Kathleen



  5. #5
    Stephen Lee Guest

    Re: Combo Boxes and Data Binding in VB.NET


    Hi Kathleen

    I believe I've fixed the issues below. Binding the combo to "SelectedValue"
    , and doing this after I had set the DataSource, DisplayMember and ValueMember,
    seems to sort the problem out. In the process, however, I have come up with
    what seems to be a bug with combos. I have a form that is broken into two
    sections:

    -A "Filter" Section, with a couple of controls and an "Apply" button, and
    -The actual detail display section, containing a grid (bound to the primary
    datatable within my dataset) and several controls.

    The second section contains a combo box that gets lookup data from a datatable,
    displaying a description and exposing a ValueMember. Its SelectedValue is
    bound to the primary datatable.

    The user selects filter values, hits "Apply" and dets a list of results records
    displayed in the grid, with the selecte record displayed in the controls.
    All pretty standard stuff. If I now change a few records by changing the
    value of the above-mentioned combo box, I will invariably eventually find
    myself in the state where I cannot set focus to any other control than the
    combo, with the exception of the grid. The combo's "Leave" event handle
    fires, followed immediately by its "Enter" handle. If I click on another
    record on the grid, I get the following error: "Object reference not set
    to an instance of an object. Do you want to correct that value?", with a
    "Unable to save values back to the underlying row".

    I have functionality happening on the CurrencyManager's position changed
    event that forces a save, but this event handle never gets executed. I also
    had "SelectedIndexChanged" and "Leave" functionality on the combo in question,
    but even with these event handles removed, the problem still intermittently
    occurs.

    The problem is intermittent; it may work for a number of records, but then
    will occur again. I cannot find any pattern as to what causes the condition
    to occur.

    Have you ever seen anything like this? It's basically forced me to manually
    handle the combo in question.

    Sorry for the lengthy posting.

    Stephen

    "Kathleen Dollard" <kathleen@mvps.org> wrote:
    >Stephen,
    >
    >> 1.) With one such combo on a dataform, every record I navigate to, not

    >including
    >> the first record, is marked as "edited", even when I have simply navigate
    >> onto the rcord then immediately navigated off to another record. What

    I
    >mean
    >> to say here is that doing a GetChanges() on the dataset reveals that this
    >> record as being marked as "edited". This seems wrong. The record should
    >> not be marked as such until I actually make changes.

    >
    >Can you first carefully check the case of all properties (such as "Text"

    vs
    >"text"). There is a known bug there. If it continues to appear edited, can
    >you post your code and I will look at it.
    >
    >> 2.) If I do edit the record by selecting another value from the combo,

    >once
    >> I've selected the new value, and the drop down list has disappeared, I

    get
    >> a blank combo text value. Once focus moves to a nother control, the newly
    >> selected value is displayed, and the underlying datarow takes the

    >newly-selected
    >> value.

    >
    >I am sorry, I didn't follow this. One possiblity is the need for the
    >EndCurrentEdit in the Validation event, but as I don't understand your
    >scenario here, I don't know if that is the solution
    >
    >Kathleen
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center