使用Textbox过滤DataGridView? (VB.net) [英] Filter DataGridView using Textbox? (VB.net)

查看:90
本文介绍了使用Textbox过滤DataGridView? (VB.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我正在使用Microsoft Visual Studio 2005.



如何在文本框中键入时过滤DataGridView?

这是我现在的表:

用户名密码
abc 123 y
def 456 y
ghi 879 y
xyz 111 n
uvw 222 n





所以当我在我的文本框abc上输入时,我想自动过滤具有该用户名的DGV。所以它只会显示:

用户名密码为
abc 123 y





提前致谢! :)



< edit>我没有制作任何绑定源。

这是我的代码:

  Dim  con 作为  clsConn 
Dim sqlstring 作为 字符串



 公共  Sub  LoadQueue()
sqlstring = < span class =code-string> SELECT * FROM Users
DGVqueue.DataSource = con.ReturnQuery(sqlstring)
结束 Sub





我的班级:

  Imports  System.Data 
< span class =code-keyword> Imports System.Data.SqlClient

Public 柯乐s clsConn
Dim connectionString = 数据源= ****** \ SQLEXPRESS;初始目录= MySystem; Integrated Security = true
Dim sqlCommandQS As SqlCommand
< span class =code-keyword> Dim sqlAdapterQS As SqlDataAdapter
Dim sqlBuilderQS 作为 SqlCommandBuilder
Dim sDsQS As DataSet
Dim sTableQS As DataTable
Dim connectionQS 作为 SqlConnection(connectionString)

函数 ReturnQuery( ByRef strsql As < span class =code-keyword> String ) As DataTable
尝试
Dim connectionQS As SqlConnection(connectionString)
connectionQS.Open()
sqlCommandQS = SqlCommand(strsql ,connectionQS)
sqlAdapterQS = SqlDataAdapter(sqlCommandQS)
sqlBuilderQS = SqlCommandBuilder (sqlAdapterQS)
sDsQS = DataSet()

sqlAdapterQS.Fill(sDsQS)
sTableQS = sDsQS.Tables ( 0
connectionQS.Close()
返回 sTableQS
Catch ex As 异常
connectionQS.Close()
返回 Nothing
MsgBox(ex.Message)
结束 尝试
结束 功能
结束

解决方案

在你的文本框中尝试如下textChanged事件

 私人  Sub  TextBox1_TextChanged(发件人作为 对象,e  As  EventArgs)句柄 TextBox1.TextChanged 
sqlstring = SELECT * FROM Users [username]赞'%'& TextBox1.Text)& %'
DGVqueue.DataSource = con.ReturnQuery(sqlstring)
End Sub < /跨度>


Hello!
I am using Microsoft Visual Studio 2005.

How do I filter a DataGridView while typing on textbox?
This is my table right now:

username     password     rated
  abc          123          y
  def          456          y
  ghi          879          y
  xyz          111          n
  uvw          222          n



So when i type on my textbox "abc" i want to automatically filter the DGV who has that username. So it will only show:

username     password     rated
  abc           123         y



Thanks in advance! :)

<edit> I did not make any bindingsource.
this is my code:

Dim con As New clsConn
Dim sqlstring As String


Public Sub LoadQueue()
    sqlstring = "SELECT * FROM Users"
    DGVqueue.DataSource = con.ReturnQuery(sqlstring)
End Sub



My Class:

Imports System.Data
Imports System.Data.SqlClient

Public Class clsConn
    Dim connectionString = "Data Source=******\SQLEXPRESS; Initial Catalog=MySystem; Integrated Security=true"
    Dim sqlCommandQS As SqlCommand
    Dim sqlAdapterQS As SqlDataAdapter
    Dim sqlBuilderQS As SqlCommandBuilder
    Dim sDsQS As DataSet
    Dim sTableQS As DataTable
    Dim connectionQS As New SqlConnection(connectionString)

    Function ReturnQuery(ByRef strsql As String) As DataTable
        Try
            Dim connectionQS As New SqlConnection(connectionString)
            connectionQS.Open()
            sqlCommandQS = New SqlCommand(strsql, connectionQS)
            sqlAdapterQS = New SqlDataAdapter(sqlCommandQS)
            sqlBuilderQS = New SqlCommandBuilder(sqlAdapterQS)
            sDsQS = New DataSet()

            sqlAdapterQS.Fill(sDsQS)
            sTableQS = sDsQS.Tables(0)
            connectionQS.Close()
            Return sTableQS
        Catch ex As Exception
            connectionQS.Close()
            Return Nothing
            MsgBox(ex.Message)
        End Try
    End Function
End Class

解决方案

try like below in your textbox textChanged event

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        sqlstring = "SELECT * FROM Users [username] Like '%" & TextBox1.Text) & "%'""
        DGVqueue.DataSource = con.ReturnQuery(sqlstring)
End Sub


这篇关于使用Textbox过滤DataGridView? (VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆