Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在Gridview中显示数据库中的全部记录,并使用textbox进行过滤?_Asp.net_Vb.net_Visual Studio 2008 - Fatal编程技术网

Asp.net 如何在Gridview中显示数据库中的全部记录,并使用textbox进行过滤?

Asp.net 如何在Gridview中显示数据库中的全部记录,并使用textbox进行过滤?,asp.net,vb.net,visual-studio-2008,Asp.net,Vb.net,Visual Studio 2008,如何在Gridview中显示数据库中的全部记录,以及如何使用文本框进行过滤?有多种方法可以做到这一点,而解决问题的方法可能取决于许多相互关联的因素;每次用户执行搜索时将返回多少条记录?默认情况下,列表应该加载所有记录,并且仅在请求时过滤,还是应该加载emply,并且仅返回请求的记录?在合理的使用条件下,DGV是否会在搜索后加载整个表内容,或仅加载少数记录 这些和其他考虑因素可能会也可能不会影响您决定填充和执行筛选操作的方式 这个例子是一个非常基本的方法,没有使用ADO.NET的一些细节,比如Da

如何在Gridview中显示数据库中的全部记录,以及如何使用文本框进行过滤?

有多种方法可以做到这一点,而解决问题的方法可能取决于许多相互关联的因素;每次用户执行搜索时将返回多少条记录?默认情况下,列表应该加载所有记录,并且仅在请求时过滤,还是应该加载emply,并且仅返回请求的记录?在合理的使用条件下,DGV是否会在搜索后加载整个表内容,或仅加载少数记录

这些和其他考虑因素可能会也可能不会影响您决定填充和执行筛选操作的方式

这个例子是一个非常基本的方法,没有使用ADO.NET的一些细节,比如DataSet、TableADapter等等。相反,它将作为beck end操作执行过滤,提交参数化查询并将结果集作为DataTable返回,然后可以将其直接设置为DGV控件的数据源。如果这引发了一场争论,我不会感到惊讶,我的意思是,关于使用ADO.NET检索数据的方式和方法的争论

无论如何,还有其他选择,鉴于我对您的设计了解有限,我无法确定什么是最实用和/或最有效的。一个示例是将完全填充的DataTable作为表单的私有类成员检索,然后对其执行DataView.RowFilter操作,使用输出作为网格的数据源。这也有利弊;首先,对于大小有限的结果集,或者对于后端数据访问遇到性能瓶颈的情况,它可能会更好

最后一个注意事项——我在这里的愚蠢示例本质上是每次文本框中的文本发生变化时都会细化搜索。根据您的需要,您可能希望对其进行设置,以便在用户按下按钮或enter键之前不会执行搜索

注意:这只是一个示例,任何类似的代码都应该通过异常处理进行改进,并根据设计的性能要求进行调整

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim dgv As DataGridView = Me.DataGridView1
    Me.UpdateSearch("")

End Sub

'A Function which returns a DataTable, to be used as the DataSource for a DataGridView:
Private Function ClientsDataTable(ByVal SearchCriteria As String) As DataTable


    'The default popoulation of your Grid Control will determine how you 
    'contruct your SQL and/or set up you parameter(s); If you want it BLANK until the user enters a value in 
    'your textbox, you will need to modify some of the logic here . . . THIS extra-simple example
    'fills the DGV with ALL records in the table if no parameter (and empty String) is passed:
    Dim SQL As String = _
    "SELECT ClientID, LastName, FirstName " & _
    "FROM tblClient " & _
    "WHERE LastName Like @LastName & '%'"

    Dim dt As New DataTable

    Using cn As New OleDb.OleDbConnection(My.Settings.CreateThisConnection)
        Using cmd As New OleDb.OleDbCommand(SQL, cn)
            cmd.Parameters.AddWithValue("@LastName", SearchCriteria)

            cn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader
            dt.Load(dr)
            dr.Close()
            cn.Close()
        End Using
    End Using

    Return dt

End Function

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Me.UpdateSearch(Me.TextBox1.Text.Trim)
End Sub

Private Sub UpdateSearch(ByVal SearchCriteria As String)
    Me.DataGridView1.DataSource = ClientsDataTable(SearchCriteria)
End Sub
End Class

不确定数据库中的整条记录是什么意思,但如果希望每条记录都有整行,请使用SELECT*from TableName。Etc而不是选择字段1、字段2。