使用文本框(vb.net)在datagridview中搜索列 [英] Search through columns in a datagridview using textbox (vb.net)

查看:1024
本文介绍了使用文本框(vb.net)在datagridview中搜索列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用文本框在datagridview中搜索列?我使用vb.net 2010.我有一个数据源的Datagridview。下面是我的填充我的datagridview的代码。 gridview将有4列。

  Private Sub LoadProducts()
Dim CS As String = ConfigurationManager.ConnectionStrings(使用con As SqlConnection =新的SqlConnection(CS)
Dim da As SqlDataAdapter =新的SqlDataAdapter(sp_NET_GetProducts_CompanyID,con)
da.SelectCommand($)
da.SelectCommand(CSMS.MySettings.ResortDBConnectionString)ConnectionString
.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 Sub
pre>

要求:在我的表单中,我将有一个文本框按钮。文本框将提供搜索字符串。我需要一种方法来突出显示一个字符串时的行。



我不想打开另一个连接来搜索数据集上的字符串。可以在datagridview中直接搜索字符串值吗?

解决方案

这里有一个示例代码来做你想要的: / p>

  Dim toSearch As String =this
Dim colNum As Integer = 0
Dim res = ds。 Table(Products)。AsEnumerable.Where(Function(x)x.Item(colNum).ToString()= toSearch).ToArray
对于每个项目在res
Dim curRow As Integer = ds。 Table(Products)。Rows.IndexOf(item)
dgvSuppliers.Rows(curRow).DefaultCellStyle.BackColor = Color.Yellow
下一个

上面的代码在表的第一列中查找字符串this 产品并将匹配的行的 BackColor 更改为黄色。



注意:这个答案打算以通常在数据源中搜索术语的方式回答OP的问题,就是通过依赖一个查询。而且,人们倾向于喜欢涉及较少线数的解决方案。这两个原因解释了为什么我依靠这种方法(这与OP的缄默)。 OP已经决定回答自己,他认为更好。我个人喜欢像他发布的那样的迭代解决方案(尽管我认为这种方法对于任何使用 DataGridView 的人来说是显而易见的)。在任何情况下,没有任何事实可以说先验,哪个选项更有效率,而不知道确切的条件(大小)。本说明的重点突出表明,我不建议定期依赖基于LINQ的方法,只是写了OP显然正在寻找(不幸的是,我很难解释一个人没有解释的期望明确寻找什么,避免任何形式的沟通)。


How do I search through columns in a datagridview using a textbox? I'm using vb.net 2010. I have a Datagridview with a data source. Below is my code for populating my datagridview. The gridview will have 4 columns.

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

Requirements: In my form I will have a textbox and button. The textbox will supply the search string. I need a way to highlight the row when a string is found.

I don't want to open another connection just to search for a string on a dataset. Is it possible to search for string values directly in the datagridview?

解决方案

Here you have a sample code doing what you want:

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

The code above looks for the string "this" in the first column of Table "Products" and change the BackColor of the matched rows to Yellow.

NOTE: this answer intends to reply the OP's question in the way usually "searching a term in a datasource" is understood, that is, by relying on a query. Also, people tend to prefer solutions involving a lower number of lines. These two reasons explain why I relied on this approach (this together with the OP's muteness). The OP has decided to answer himself what he has considered better. I personally prefer iterative solutions like the one he posted (although I consider that this approach is evident to anyone using a DataGridView). In any case, nothing can be said a priori about which option is more efficient without knowing the exact conditions (size). The whole point of this note is highlighting that I don't recommend relying on LINQ-based approaches on a regular basis, just wrote what the OP was apparently looking for (unfortunately, I am pretty bad at interpreting the expectations of a persons not explaining clearly what is looking for and avoiding any kind of communication).

这篇关于使用文本框(vb.net)在datagridview中搜索列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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