如何使用textbox vb.net在datagridview中移动到特定的行/单元格 [英] How to move into specific row/cell in datagridview using textbox vb.net

查看:381
本文介绍了如何使用textbox vb.net在datagridview中移动到特定的行/单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的程序。

如果我点击顶部表单上的按钮搜索,新表单将显示如下。

If I click the button search on the top form, a new form will show like this.

如果我将一个数据放在文本框内,然后按查找程序会找到最近的数据并将颜色放在上面。像这样。

If I put a data inside the textbox and press find the program will find the nearest data and put color on it. like this.

这是代码因为(由我知道的最好的程序员之一)

and this is the code for that (Given by the one of the best programmer that I know)

 Try
            If TextBox1.Text = "" Then
                MsgBox("Nothing to search.")
            Else
                Dim drow() As DataRow
                drow = dt.Select("ItemCode LIKE '%" & TextBox1.Text & "%' OR Description LIKE '%" & TextBox1.Text & "%'")
                If drow.Count > 0 Then
                    'THIS WILL LOOP THE GRID BASED ON THE RECORDS FOUND BY TXTSEARCH.TEXT
                    For Each row As DataRow In drow
                        Dim ItemCodeStr As String = row.Item("ItemCode").ToString
                        For Each dgrow As DataGridViewRow In Variance.DataGridView1.Rows
                            If ItemCodeStr = dgrow.Cells(2).Value.ToString Then
                                dgrow.DefaultCellStyle.BackColor = Color.Pink
                            End If
                        Next
                    Next
                Else
                    MsgBox("There are no matches found.", MsgBoxStyle.Information, "Result")
                End If
            End If
        Catch
        End Try

现在我的问题是如何使搜索成为选择,而不是着色数据?

Now my question is how can i make the search a selection instead of coloring the data?

我的意思是这个。

如果我在文本框中输入一个字,则蓝色或选择会去那里。如果数据在底部,那么蓝色或选择将会转到底部。

If my type a word in textbox the Blue color or Selection will go there. If the data is in the bottom then the Blue color or Selection will go to the bottom something like that.

TYSM for future help

TYSM for future help

推荐答案

我在这里使用了LINQ ,所以你现在可以放弃你的 datatable 你正在使用 .Select() function

I used LINQ here so you can now discard your datatable that you are using for .Select() function

您可以通过运行时进行操作,但建议您在设计时将其设置为 datagridview 的属性。设置这些属性:

It's either you do it thru run-time but I recommend just set it to the properties of the datagridview at design-time. Set these properties:

DataGridView1.MultiSelect = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

然后在搜索过程之外声明一个变量。当选择到达网格的末尾时,这将被使用:

Then declare a variable outside of your search procedure. This will be used when the selection reached the end of the grid:

Dim reachedEnd As Boolean = False

现在的过程:

    Dim CurrIndex As Integer = 0
    If reachedEnd Then
        CurrIndex = -1
        reachedEnd = False
    Else
        CurrIndex = DataGridView1.CurrentRow.Index
    End If

    Dim resultSet = From drow As DataGridViewRow In Me.DataGridView1.Rows _
                Where (drow.Cells(2).Value.ToString.ToUpper Like "*" & txtSearch.Text.ToUpper & "*" _
                       Or drow.Cells(3).Value.ToString.ToUpper Like "*" & txtSearch.Text.ToUpper & "*") _
                And (drow.Index > CurrIndex) _
                Select drow.Index

    If resultSet.Count > 0 Then
        With DataGridView1
            .Rows(resultSet.ToList(0)).Selected = True
            .CurrentCell = .Rows(resultSet.ToList(0)).Cells(0)
        End With
    Else
        MsgBox("There are no records found.", MsgBoxStyle.Information, "Result")
        reachedEnd = True
    End If

这将搜索与 ItemCode 的搜索条件的可能匹配, 描述然后,如果再次点击您的查找按钮,它将进入下一个可能的匹配。如果达到网格的末尾,它将重新开始搜索顶部。

This will search for possible matches with your search criteria for ItemCode and Description then if you click your Find button again, it will go to the next possible match. If it reached the end of the grid, it will start again the searching at the top.

这就是全部。现在要简单得多(谢谢LINQ!)。

That's all. It's much shorter and simpler now (thanks LINQ!).

这篇关于如何使用textbox vb.net在datagridview中移动到特定的行/单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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