我在SQL Server中有一个存储过程,该存储过程将两个表返回到我的VB.Net表单。这两个表填充了两个网格。我的数据表是父母和孩子。我想按第一个网格过滤第二个网格。换句话说,当用户在第一网格中选择一个记录时,必须通过考虑以编程方式声明的主键来过滤第二网格。 但这不起作用! 我的代码是这样的:
Dim DAContractorDateTreeView As New SqlDataAdapter("spTimeSheetReportDateTree", My.Settings.SyncConnectionString)
Dim DSContractorDate As New DataSet
Dim fkeyConstraint As ForeignKeyConstraint
Dim Binding1, Binding2 As New BindingSource
Sub LoadContractorsDate(MyParameter As Integer)
Try
DAContractorDateTreeView.SelectCommand.CommandType = CommandType.StoredProcedure
'Some Codes for adding parameters
DAContractorDateTreeView.Fill(DSContractorDate)
'Declare parent column and child column variables.
Dim ParentColumn, ChildColumn As DataColumn
Dim primaryKey(1) As DataColumn
primaryKey(0) = DSContractorDate.Tables(2).Columns("TiSheID")
DSContractorDate.Tables(2).PrimaryKey = primaryKey
'Set parent and child column variables.
ParentColumn = DSContractorDate.Tables(0).Columns("TiSheID")
ChildColumn = DSContractorDate.Tables(1).Columns("TiSheID")
fkeyConstraint = New ForeignKeyConstraint("FKConstraintNaID", ParentColumn, ChildColumn)
'Set null values when a value is deleted.
fkeyConstraint.DeleteRule = Rule.Cascade
fkeyConstraint.UpdateRule = Rule.Cascade
fkeyConstraint.AcceptRejectRule = AcceptRejectRule.Cascade
'Add the constraint, and set EnforceConstraints to true.
DSContractorDate.Tables(1).Constraints.Add(fkeyConstraint)
DSContractorDate.EnforceConstraints = True
Binding1.DataSource = DSContractorDate.Tables.Item(0)
GridControlExit.DataSource = Binding1
Binding1.DataMember = "TiSheID"
Binding2.DataMember = "FKConstraintNaID" 'fkeyConstraint.ConstraintName
MsgBox(fkeyConstraint.ConstraintName)
Binding2.DataSource = Binding1
GridControlDetail.DataSource = Binding2
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
从存储过程中加载数据没有问题。 请帮我。预先感谢。
答案 0 :(得分:0)
您现在可能已经找到了解决方案,但是Microsoft希望它是:
select * from parent
添加一个表适配器您会注意到,对于父级下的网格,选择父级时,子级网格会自动过滤。如果停止应用程序并检查子网格的数据源,您将看到原因。这两个网格都有作为绑定源的数据源,但是对于自动过滤的网格,相关的绑定源将其DataSource设置为父绑定源,而DataMember是datarelation的名称
您可能可以将其复制到您的手动代码中,但是要考虑开始使用数据集和表适配器。它们使您正在编写的许多代码现在变得更容易且更实用。
终点;在配置适配器时,您进行了查询“从表中选择*”。在生产中,您不会这样做;不要下载所有表格数据并在子项上进行过滤。向您的表适配器添加多个相关查询并使用它们; select * from users where lastname like @ln
并将其命名为fillbylastname,如果您想将通配符放在代码中:usersTableAdapter.FillByLastName(appDataSet.Users, "Smi%")