我不是一个程序员只是一个爱好者,试图为自己制作一个小应用程序。
我的查询是我有两个下拉列表(vb.net表单),一个是“构建器”,下一个是“项目”(两者都通过MSSQL中的“BuilderID”链接),我想过滤“项目”列出链接到所选“Builder”
的链接我的“构建器”下拉列表很好,我正在考虑第二个“项目列表”的存储过程,这是根据构建器的选择查询项目表的一种方式。
我试图编辑这个问题,但不知道要添加什么,因为玛丽必须明白我的要求才能很好地回答这个问题。
答案 0 :(得分:1)
我不确定你是如何填写你的第一个清单所以我从头开始。重要的是将ValueMember设置为ID。然后使用SelectedIndexChanged事件,您可以获取下一个查询的构建器ID。同样的理论适用于填充网格。非常可爱的宝贝!
Imports System.Data.SqlClient
Public Class DWGBrowse
Public Property FinishedLoading As Boolean = False
Private Sub DWGBrowse_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillBuildersList()
FinishedLoading = True
End Sub
Private Sub FillBuildersList()
Dim strSQL = "Select * From Builders;"
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.BuildersConnection)
Using cmd As New SqlCommand(strSQL, cn)
Try
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
dt.Load(dr)
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End Using
End Using
BuilderBox.DataSource = dt
BuilderBox.DisplayMember = "BuilderName" 'Whatever your column is called
BuilderBox.ValueMember = "BuilderId" 'the name of your ID column
BuilderBox.SelectedIndex = -1
End Sub
Private Sub BuilderBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BuilderBox.SelectedIndexChanged
If Not FinishedLoading Then Exit Sub
Try
Using sqlConnection As New System.Data.SqlClient.SqlConnection(My.Settings.BuildersConnection)
Dim id As Integer = CInt(BuilderBox.SelectedValue)
Dim dt As New DataTable
Dim sqlString As String = "Select * From Projects Where BuilderID = @ID"
Using cmd As New System.Data.SqlClient.SqlCommand(sqlString, sqlConnection)
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id
ProjectBox.DataSource = Nothing
sqlConnection.Open()
Using dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
dt.Load(dr)
ProjectBox.DisplayMember = "ProjectName"
ProjectBox.ValueMember = "ProjectID"
ProjectBox.DataSource = dt
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ProjectBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ProjectBox.SelectedIndexChanged
Dim id As Integer = CInt(ProjectBox.SelectedValue)
Dim dt As New DataTable
Dim strSQL As String = "Select * From Drawings Where ProjectID = @ID"
Using cn As New SqlConnection(My.Settings.BuildersConnection)
Using cmd As New SqlCommand(strSQL, cn)
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = CInt(ProjectBox.SelectedValue)
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
dt.Load(dr)
End Using
End Using
End Using
DataGridView1.DataSource = dt
End Sub
End Class