编辑的代码。如何使用vb.net从asp.net的下拉列表中显示gridview

时间:2016-01-07 05:32:57

标签: html asp.net vb.net gridview

您好我已经发布了这个问题,但是得到答案请帮我通过下拉列表显示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

1 个答案:

答案 0 :(得分:0)

您正在联接filtersql 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)