您好我已经发布了这个问题,但是得到答案请帮我通过下拉列表显示gridview。以下是vb.net中的代码
Private Sub BindGridView()
Dim filter As String = ""
If ddlOwnerDepartment.SelectedValue <> "" Then
filter = filter & " AND OwnerDepartment ='" & ddlOwnerDepartment.SelectedValue & "'"
End If
Dim table1 As DataTable = New DataTable("Details")
table1.Columns.Add("ReferenceNo")
table1.Columns.Add("Title")
table1.Columns.Add("Counterparty")
table1.Columns.Add("OwnerDepartment")
table1.Columns.Add("Status")
table1.Columns.Add("CreatedBy")
table1.Columns.Add("CreatedOn")
table1.Columns.Add("link")
table1.Rows.Clear()
Dim conSQL As New SqlConnection(conStrDRS)
conSQL.Open()
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT Title, ReferenceNo,OwnerDepartment,CreatedBy,CreatedOn,Status,Counterparty " & _
" FROM [dbo].[Registration]" & filter)
cmdSQL.Connection = conSQL
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
Dim myDataSet As New DataSet()
adptSQL.Fill(myDataSet)
conSQL.Close()
gvDetails.DataSource = myDataSet.Tables(0) 'dvAlert
gvDetails.DataBind()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
BindGridView()
End Sub
答案 0 :(得分:0)
您正在联接filter
和sql query
的结尾,因为filter
具有您在查询结束时添加的相同值。{/ p>
也不要使用Parameterized SQL
连接值。像这样在你的SQL查询中添加参数。
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT Title, ReferenceNo,OwnerDepartment,CreatedBy,CreatedOn,Status,Counterparty " &
" FROM [dbo].[Registration] Where OwnerDepartment=@OwnerDepartment");
cmdSQL.Paramters.AddWithValue("@OwnerDepartment",ddlOwnerDepartment.SelectedValue);
修改强>
Private Sub BindGridView(bool showAll)
Dim filter As String = ""
If ddlOwnerDepartment.SelectedValue <> "" Then
filter = filter & " AND OwnerDepartment ='" & ddlOwnerDepartment.SelectedValue & "'"
End If
Dim table1 As DataTable = New DataTable("Details")
table1.Columns.Add("ReferenceNo")
table1.Columns.Add("Title")
table1.Columns.Add("Counterparty")
table1.Columns.Add("OwnerDepartment")
table1.Columns.Add("Status")
table1.Columns.Add("CreatedBy")
table1.Columns.Add("CreatedOn")
table1.Columns.Add("link")
table1.Rows.Clear()
Dim conSQL As New SqlConnection(conStrDRS)
conSQL.Open()
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT Title, ReferenceNo,OwnerDepartment,CreatedBy,CreatedOn,Status,Counterparty FROM [dbo].[Registration] ";
if showAll==false then
cmdSQL += "Where OwnerDepartment=@OwnerDepartment");
cmdSQL.Paramters.AddWithValue("@OwnerDepartment",ddlOwnerDepartment.SelectedValue);
End If
cmdSQL.Connection = conSQL
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
Dim myDataSet As New DataSet()
adptSQL.Fill(myDataSet)
conSQL.Close()
gvDetails.DataSource = myDataSet.Tables(0) 'dvAlert
gvDetails.DataBind()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
BindGridView(true)
End Sub
现在当您从dropdown search
调用此函数时,将该函数的false传递给BindGridView(false)