如何为 TableAdapter 参数设置值 [英] How To Set a Value to a TableAdapter Parameter
问题描述
我希望能够有两个组合框,其中一个是第二个的父级或所有者.这意味着每当我在第一个 ComboBox
中选择一个值时,第二个 ComboBox
将过滤它的结果以显示与第一个 ComboBox
.
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
.
例如:
注意:这个例子已经以编程方式完成了......我想弄清楚如何使用 Visual Studio 的用户界面来完成它
我有一个包含两个 DataTable 的数据集,如下所示:
I have a dataset with two DataTables like so:
如您所见,我的 BakerySubSectionsTableAdapter
中有一个名为 @FK_BakerySection 的参数.我想将其链接到 BakerySection's
PK_BakerySection 属性.
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.
这是我目前的结果:
在我的 TableAdapter 中使用以下查询:
Using the following query in my TableAdapter:
那么...我们如何使用用户界面为参数设置值?
So ... How do we set a value to a parameter using the User Interface?
推荐答案
如果您在两个表之间添加 DataRelation
就很容易(IIRC 您可以在 DataSet 设计器中简单地执行此操作).
It's easy if you add a DataRelation
between your two tables (IIRC you can simply do this in the DataSet designer).
那么您只需将第二个 ComboBox
的 DisplayMember
设置为 ParentTable.NameOfRelation.NameToDisplay
.
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()
只需确保您的 BakerySubSections
已完全填充(不需要参数).
Just make sure your BakerySubSections
is completly filled (no need for the parameter).
这篇关于如何为 TableAdapter 参数设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!