使用VB.NET中的文本框过滤datagridview中的数据 [英] Filter data in datagridview by using a textbox in VB.NET

查看:78
本文介绍了使用VB.NET中的文本框过滤datagridview中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有人可以告诉我是否有办法使用文本框从datagridview过滤数据?



谢谢。



我尝试了什么:



 Private Sub Button3_Click(sender As Object,e As EventArgs)处理Button3.Click 
Dim str As String =Data Source =; Initial Catalog = user; Persist Security Info = True; User ID = sa; Password =
Dim con As新的SqlConnection(str)
Dim com As String =从AWB_CHQ_RETOUR选择DATE,登录,操作,Nbr_docs,tps_traite,moy_doc_hr,moy_sec_doc,taux_err
Dim Adpt As New SqlDataAdapter(com,con)
Dim ds As New DataSet()
Adpt.Fill(ds,AWB_CHQ_RETOUR)
DataGridView1.DataSource = ds.Tables(0)
End Sub< pre>





这是我用来包装DatagridView的代码,我有六个类似的按钮,从其他表填充我的datagrid s。

解决方案

是的。



Google的研究资料 [ ^ ]




实现这一目标的方法很少:

1)通过选择 [ ^ ]方法。

请参阅:

数据集中的过滤和排序 [ ^ ]

如何:直接在数据表中过滤和排序 [ ^ ]



2)使用过滤方法 [ ^ ] for BindingSource(Caroline - CHill60 [ ^ ]已经提到过该方法)



3)使用SqlCommand从数据库中获取过滤后的数据

您必须使用与用于将数据填充到DataGridView相同的代码。唯一不同的是 SELECT 语句。

  SELECT   DATE ,登录,操作,Nbr_docs,tps_traite,moy_doc_hr,moy_sec_doc,taux_err 
来自 AWB_CHQ_RETOUR
WHERE Login = ' SomeLoginHere'<使用 Linq to DataSet [ ^ ]

请参阅:

LINQ to DataSet示例 [ ^ ]

C#中的101个LINQ样本 [ ^ ]

创建DataTable从查询(LINQ到DataSet) [ ^ ]



 DataTable dt =  DirectCast (DataGridView1.DataSource,DataTable)
Dim qry = dt.AsEnumerable()_
.Where(函数(x)x.Field( Of String )( 登录)= SomeLoginHere
Dim boundTable As DataTable = qry.CopyToDataTable
DataGridView1.DataSource = boundTable


Hi Everyone , please someone can tell me if there is a way to filter data from datagridview using a Textbox ?

thank you.

What I have tried:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim str As String = "Data Source=;Initial Catalog=user;Persist Security Info=True;User ID=sa;Password="
        Dim con As New SqlConnection(str)
        Dim com As String = "Select DATE ,Login ,Operation ,Nbr_docs ,tps_traite ,moy_doc_hr ,moy_sec_doc ,taux_err   from AWB_CHQ_RETOUR "
        Dim Adpt As New SqlDataAdapter(com, con)
        Dim ds As New DataSet()
        Adpt.Fill(ds, "AWB_CHQ_RETOUR")
        DataGridView1.DataSource = ds.Tables(0)
    End Sub <pre>



this is the code I use to poulate the DatagridView , and I have six buttons similar to this , that populate my datagrid from other tables .

解决方案

Yes there is.

Research material from Google[^]

[EDIT - here is a link to the MSDN documentation Building a Drop-Down Filter List for a DataGridView Column Header Cell[^]


There's few ways to achieve that:
1) using filter on DataSet/DataTable through the Select[^] method.
See:
Filtering and Sorting in Datasets[^]
How to: Filter and Sort Directly in Data Tables[^]

2) using Filter method[^] for BindingSource (Caroline - CHill60[^] already mentioned about that method)

3) using SqlCommand to grab filtered data from database
You have to use the same code as you use to populate data to DataGridView. The only differ is in SELECT statement.

SELECT DATE ,Login ,Operation ,Nbr_docs ,tps_traite ,moy_doc_hr ,moy_sec_doc ,taux_err
from AWB_CHQ_RETOUR
WHERE Login = 'SomeLoginHere'



4) using Linq to DataSet[^]
See:
LINQ to DataSet Examples[^]
101 LINQ Samples in C#[^]
Creating a DataTable From a Query (LINQ to DataSet)[^]

DataTable dt = DirectCast(DataGridView1.DataSource, DataTable)
Dim qry = dt.AsEnumerable() _
  .Where(Function(x) x.Field(Of String)("Login")="SomeLoginHere")
Dim boundTable As DataTable = qry.CopyToDataTable
DataGridView1.DataSource = boundTable


这篇关于使用VB.NET中的文本框过滤datagridview中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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