AdvancedHMI Software

General Category => Open Discussion => Topic started by: bachphi on March 27, 2017, 02:24:45 PM

Title: DataSet and ComboBox
Post by: bachphi 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:

(http://s16.postimg.org/5gfq0oool/Dataset_Combo_Box.jpg)

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.
Title: Re: DataSet and ComboBox
Post by: Phrog30 on March 27, 2017, 07:51:12 PM
You can simply drag the data source onto the form and everything is done automatically.
Title: Re: DataSet and ComboBox
Post by: bachphi on March 27, 2017, 08:23:26 PM
If you read it carefully, that simply action was  already done on Page2.
Title: Re: DataSet and ComboBox
Post by: bachphi 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)
Title: Re: DataSet and ComboBox
Post by: Archie 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
Title: Re: DataSet and ComboBox
Post by: bachphi 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?
Title: Re: DataSet and ComboBox
Post by: Archie 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.