使用textbox(vb.net)搜索datagridview中的列
如何使用文本框搜索datagridview中的列?我正在使用vb.NET2010。我有一个带有数据源的Datagridview。下面是填充datagridview的代码。gridview将有4列使用textbox(vb.net)搜索datagridview中的列,vb.net,search,datagridview,find,bind,Vb.net,Search,Datagridview,Find,Bind,如何使用文本框搜索datagridview中的列?我正在使用vb.NET2010。我有一个带有数据源的Datagridview。下面是填充datagridview的代码。gridview将有4列 Private Sub LoadProducts() Dim CS As String = ConfigurationManager.ConnectionStrings("HRMS.My.MySettings.ResortDBConnectionString").ConnectionString
Private Sub LoadProducts()
Dim CS As String = ConfigurationManager.ConnectionStrings("HRMS.My.MySettings.ResortDBConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(CS)
Dim da As SqlDataAdapter = New SqlDataAdapter("sp_NET_GetProducts_CompanyID", con)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("@CompanyID", CInt(ConfigurationManager.AppSettings("CompanyID")))
Dim ds As DataSet = New DataSet
da.Fill(ds)
ds.Tables(0).TableName = "Products"
dgvProducts.DataSource = ds.Tables("Products")
dgvProducts.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgvProducts.AllowUserToResizeColumns = True
dgvProducts.Refresh()
End Using
End Sub
要求:在我的表格中,我将有一个文本框
和按钮
。文本框将提供搜索字符串。我需要一种在找到字符串时突出显示行的方法
我不想仅仅为了在数据集上搜索字符串而打开另一个连接。是否可以直接在datagridview中搜索字符串值?这里有一个示例代码,可以执行您想要的操作:
Dim toSearch As String = "this"
Dim colNum As Integer = 0
Dim res = ds.Tables("Products").AsEnumerable.Where(Function(x) x.Item(colNum).ToString() = toSearch).ToArray
For Each item In res
Dim curRow As Integer = ds.Tables("Products").Rows.IndexOf(item)
dgvSuppliers.Rows(curRow).DefaultCellStyle.BackColor = Color.Yellow
Next
上面的代码在表“产品”
的第一列中查找字符串“this”
,并将匹配行的背景色更改为黄色
注:此答案旨在以通常理解的“在数据源中搜索术语”的方式回答OP的问题,即依赖于查询。此外,人们倾向于选择包含较少行数的解决方案。这两个原因解释了我为什么依赖这种方法(这与OP的静音一起)。OP决定回答他认为更好的问题。我个人更喜欢他所发布的一个迭代解决方案(尽管我认为这种方法对于使用<代码> DATGRIDVIEW 的任何人都是显而易见的)。在任何情况下,如果不知道确切的条件(大小),就不能先验地说哪个选项更有效。本说明的重点是强调,我不建议定期依赖基于LINQ的方法,只是写下OP显然在寻找的东西(不幸的是,我很不善于解释一个人的期望,没有清楚地解释他在寻找什么,也没有避免任何形式的交流).您可以根据需要使用。因此,您的代码如下所示
- 宣布(公开)
- 使用BindingSource填充dgvProducts
- 转到txtboxSerach并在其TextChanged事件上
- 转到txtboxSerach并在其验证事件上
- 结果
我的DataGridView如下所示
ID Name Other
---------------
0 Abcd 321
1 Abdc 546
2 Bcdsf 1005
当我开始在txtBoxSerach中键入字母A
时
是否可以在此处添加WHERE
语句?@ChadPatrick
Private Sub LoadProducts()
Dim CS As String = ConfigurationManager.ConnectionStrings("HRMS.My.MySettings.ResortDBConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(CS)
Dim da As SqlDataAdapter = New SqlDataAdapter("sp_NET_GetProducts_CompanyID", con)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("@CompanyID", CInt(ConfigurationManager.AppSettings("CompanyID")))
da.Fill(ds)
ds.Tables(0).TableName = "Products"
'/*--------------------------------------------
bndSourceGrid.DataSource = ds.Tables("Products")
dgvProducts.DataSource = bndSourceGrid
'/*--------------------------------------------
dgvProducts.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgvProducts.AllowUserToResizeColumns = True
dgvProducts.Refresh()
End Using
End Sub
Private Sub txtboxSeracht_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtSearchCust.TextChanged
'/*here "Name" is the column that you want filter/search
bndSourceGrid.Filter = String.Format("{0} LIKE '{1}%'", "Name",
txtboxSerach.Text)
'/* sorting method ascending/descending
bndSourceGrid.Sort = "Name ASC"
End Sub
Private Sub txtboxSerach_Validated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtboxSerach.Validated
If txtboxSerach.Text = String.Empty Then
bndSourceGrid.RemoveFilter()
Else
bndSourceGrid.Filter = String.Format("{0} = '{1}'", "Name",
txtboxSerach.Text)
bndSourceGrid.Sort = "Name ASC"
End If
End Sub
ID Name Other
---------------
0 Abcd 321
1 Abdc 546
2 Bcdsf 1005
ID Name Other
---------------
0 Abcd 321
1 Abdc 546