使用行过滤器从表中获取特定名称

时间:2018-05-15 17:49:20

标签: filter row

有一个代码可以将查询(fname,lname和location)中的字段过滤到DataGridView中:

Dim DV As New DataView(dbdataset1)
DV.RowFilter = String.Format("fname like '%" & Me.tbSearch.Text.Trim & "%'")
DataGridView.DataSource = dbdataset1

过滤器可以找到lnamelocation但不能fname

enter image description here

这是填充字段的屏幕截图

1 个答案:

答案 0 :(得分:0)

对于DataGridView元素,我可以告诉你是在ASP.NET 2.0或更低版本。无论如何,我让它在ASP.NET 4.7上工作:

ASPX文件:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        </div>

        <asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Search" />
    </form>
</body>
</html>

代码背后(VB.Net):

Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

    Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load

    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim dbdataset1 As New DataTable
        Dim dr As DataRow

        dbdataset1.Columns.Add("fname")

        For i As Integer = 1 To 3
            dr = dbdataset1.NewRow()

            dr("fname") = "picture" + i.ToString()
            dbdataset1.Rows.Add(dr)
        Next

        For i As Integer = 1 To 3
            dr = dbdataset1.NewRow()

            dr("fname") = "document" + i.ToString()
            dbdataset1.Rows.Add(dr)
        Next

        Dim DV As New DataView(dbdataset1)

        DV.RowFilter = String.Format("fname like '%" & Me.tbSearch.Text.Trim & "%'")

        Me.GridView1.DataSource = DV
        Me.GridView1.DataBind()
    End Sub
End Class

只需将GridView1替换为您的DataGrdiView

enter image description here enter image description here

更新:

因此,用户通过删除并再次将Texbox控件添加到表单中来修复过滤器。