Author Topic: DataSet and ComboBox  (Read 1811 times)

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
DataSet and ComboBox
« on: March 27, 2017, 02:24:45 PM »
I created a local DB file [DB_Product.mdf]  and a DataSet [DB_ProductDataSet.xsd]
Inside the db, I created a table [TB_PartSpec] that contains: Id, PartName, PartNumber, PressureLoLim, ....

On the MainForm, an Add/Change button will ShowDialog of Page2, where you can add/change Part specs.
Also on the mainform is a combobox to select PartName. The combobox is bound to the table as shown below:



In order to display part details associated with the PartName selected from the combobox, I use its IndexChanged event:
Code: [Select]
If ComboBox1.SelectedValue IsNot Nothing Then
            Label2.Text = DB_ProductDataSet.TB_PartSpec.FindById(ComboBox1.SelectedValue).PartNumber
            Label3.Text = DB_ProductDataSet.TB_PartSpec.FindById(ComboBox1.SelectedValue).PressureLoLim
            Label4.Text = DB_ProductDataSet.TB_PartSpec.FindById(ComboBox1.SelectedValue).PressureHiLim
        End If

This works for me, and the values will be written to PLC later, but I wonder if you would have a better way to handle this.
TIA.
« Last Edit: March 27, 2017, 08:37:33 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Phrog30

  • Guest
Re: DataSet and ComboBox
« Reply #1 on: March 27, 2017, 07:51:12 PM »
You can simply drag the data source onto the form and everything is done automatically.

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: DataSet and ComboBox
« Reply #2 on: March 27, 2017, 08:23:26 PM »
If you read it carefully, that simply action was  already done on Page2.
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: DataSet and ComboBox
« Reply #3 on: March 27, 2017, 10:35:51 PM »
Here's another way:

Code: [Select]
            Dim foundRow As DataRow = DB_ProductDataSet.Tables("TB_PartSpec").Rows.Find(ComboBox1.SelectedValue)
            Label2.Text = foundRow(2)
            Label3.Text = foundRow(3)
« Last Edit: March 27, 2017, 10:42:10 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: DataSet and ComboBox
« Reply #4 on: March 28, 2017, 05:51:31 AM »
You can also use DataBinding on your labels which does not require any code:

- Select the label
- In the Properties Window, expand down DataBinding
- Go into the Text
- Select your same BindingSource you are using for the ComboxBox, except with a different column

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: DataSet and ComboBox
« Reply #5 on: March 28, 2017, 05:28:46 PM »
That's really cool!

Now if I use BasicLabel with PLCAddressValue, can the values from TableBindingSource  'write' to PLCAddress without coding?
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: DataSet and ComboBox
« Reply #6 on: March 28, 2017, 06:38:39 PM »
Now if I use BasicLabel with PLCAddressValue, can the values from TableBindingSource  'write' to PLCAddress without coding?
This should work with most of the visual controls. For binding to update properly a control has to implement IBindableComponent. I do not believe the non-visual controls, such as the DataSubscriber, implement the interface so for those it will not work.

The drivers implement the interface which will also allow you to bind property values to project settings.