How To Set a Value to a TableAdapter Parameter

只愿长相守 提交于 2019-12-10 21:15:47

问题


Goal

I want to be able to have two ComboBoxes where one is the parent or owner of the second one. This means that whenever I select a value in the first ComboBox, the second ComboBox will filter it's results to display the corresponding values related to the first ComboBox.

For example:

Note: This example has been completed programmatically... I want to figure out how to do it using the user interface of Visual Studio


Current Situation

I have a dataset with two DataTables like so:

As you can see, I have a parameter in my BakerySubSectionsTableAdapter called @FK_BakerySection. I want to link that to the BakerySection's PK_BakerySection attribute.


Current Result

Here is my current result:

Using the following query in my TableAdapter:

So ... How do we set a value to a parameter using the User Interface?


回答1:


It's easy if you add a DataRelation between your two tables (IIRC you can simply do this in the DataSet designer).

Then you only have to set the DisplayMember of your second ComboBox to ParentTable.NameOfRelation.NameToDisplay.


Here's a small, complete example:


Dim data = New DataSet()
Dim section = data.Tables.Add("Section")
section.Columns.Add("ID", GetType(Integer))
section.Columns.Add("Name", GetType(String))

Dim sub_section = data.Tables.Add("SubSection")
sub_section.Columns.Add("ID", GetType(Integer))
sub_section.Columns.Add("Name", GetType(String))
sub_section.Columns.Add("Section", GetType(Integer))

section.Rows.Add(New Object() {1, "Foo"})
section.Rows.Add(New Object() {2, "Bar"})

sub_section.Rows.Add(New Object() {1, "Sub Foo", 1})
sub_section.Rows.Add(New Object() {2, "Another Sub Foo", 1})

sub_section.Rows.Add(New Object() {3, "Sub Bar", 2})
sub_section.Rows.Add(New Object() {4, "bar bar bar", 2})
sub_section.Rows.Add(New Object() {5, "more bar", 2})

section.ChildRelations.Add("SectionToSub", section.Columns("ID"), sub_section.Columns("Section"))

Dim f = New Form()
Dim c1 = New ComboBox() With { _
    .DataSource = data, _
    .DisplayMember = "Section.Name", _
    .ValueMember = "Id" _
}
Dim c2 = New ComboBox() With { _
    .DataSource = data, _
    .DisplayMember = "Section.SectionToSub.Name", _
    .ValueMember = "Id" _
}
Dim fl = New FlowLayoutPanel()
fl.Controls.Add(c1)
fl.Controls.Add(c2)
f.Controls.Add(fl)
f.ShowDialog()

Just make sure your BakerySubSections is completly filled (no need for the parameter).



来源:https://stackoverflow.com/questions/23390646/how-to-set-a-value-to-a-tableadapter-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!