0

我想为我的标题列 [Carrier] 之一创建一个过滤器,这是我创建的 gridview 的代码:

索引.aspx:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="cr_OnSelectedIndexChanged"
                 DataSourceID="SqlDataSource1" DataTextField="CARRIER" DataValueField="CARRIER"> 
             </asp:DropDownList>
             <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:MybaseConnectionString %>" 
                 SelectCommand="SELECT DISTINCT [CARRIER] FROM [TABLE]">
                 </asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" Height="150px" Width="284px">
</asp:GridView>

索引.aspx.vb

Inherits System.Web.UI.Page
    Dim cx As New SqlConnection("Data Source=.\SQLEXPRESS;database=Mybase;Integrated Security=True;")
    Dim cmd As New SqlCommand
    Dim da As New SqlDataAdapter
    Dim dt As New DataTable

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
            cmd.Connection = cx
            cmd.CommandText = "select cl1, Carrier, cl2, cl3 from table"
            da = New SqlDataAdapter(cmd)
            da.Fill(dt)
            GridView1.DataSource = dt
            GridView1.DataBind()
        End Sub

   Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

End Sub

任何人都可以帮助请!谢谢..

4

4 回答 4

0

假设 Carrier 是 nvarchar(200) 类型的字段,并且应该在用户单击 Button1 后进行过滤(在标记中不可见),方法 Button1_Click 的主体应该如下所示:

    Dim sql As String = "select cl1, Carrier, cl2, cl3 from table where Carrier = @Carrier"

    Using cn As New SqlConnection("Data Source=.\SQLEXPRESS;database=Mybase;Integrated Security=True;"), _
      cmd As New SqlCommand(sql, cn)

      cmd.Parameters.Add("@Carrier", SqlDbTypes.NVarChar, 200).Value = DropDownList1.SelectedValue

      da = New SqlDataAdapter(cmd)
      da.Fill(dt)
      GridView1.DataSource = dt
      GridView1.DataBind()
    End Using
于 2013-06-17T08:28:22.553 回答
0

您可能需要查看RowFilter. DataGrid看看这个链接,其中包括示例代码。

于 2013-06-17T09:03:20.837 回答
0

我建议在您希望过滤的列的标题中放置一个下拉列表(在您的情况下为运营商)。然后,您可以连接下拉列表的 OnSelectedIndexChanged 事件来处理列数据的实际过滤。这种技术的一个较旧但仍然相关的示例如下:

ASP.NET GridView 列过滤

于 2013-06-17T04:49:19.770 回答
0

取一个文本框

<asp:TextBox runat="server" ID="txtSearch"/>  

按钮点击事件

 Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
            cmd.Connection = cx
            cmd.CommandText = "select cl1, Carrier, cl2, cl3 from table where Carrier='"+txtSearch.Text+"'"
            da = New SqlDataAdapter(cmd)
            da.Fill(dt)
            GridView1.DataSource = dt
            GridView1.DataBind()
        End Sub
于 2013-06-17T04:42:53.100 回答