问题循环和遍历列 [英] Problem looping and searching through column

查看:80
本文介绍了问题循环和遍历列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在做一个分配,以在datagridview列中搜索一个值.如果值与输入文本框匹配,则datagrid光标应移动到具有该搜索值的单元格,并且如果列具有多个搜索值,则光标应移动至具有该特定搜索值的下一个单元格,依此类推. ----------
column1
----------
ANATR
安东
AROUT
BERGS
AROUT
BLONP
AROUT
BONAP
---------
假设如果我们在用户第一次按下serch按钮时搜索位于3月5日,5月和7日单元格中的值AROUT,则数据网格光标应移至第3个单元格,而当用户再次单击搜索按钮时,datagrid光标应移至3号单元格.第五个单元格,当用户第三次单击搜索按钮时,数据网格光标应移至第七个单元格
我已经尝试过使用FOR循环和IF条件进行许多组合
但是默认情况下使用以下代码,FOR循环从第一行开始,一直到第七行,但不会返回第一行,并且当我们单击第六行之后单击搜索按钮时,它不会找到前两个值第三和第五行''
我如何循环回到第一行.
很难为我解释,但我希望我能传达我的观点
我的代码是

hi all,
I am working on an assignment to search a value in datagridview column. If the value matches with input textbox then datagrid cursor should move to the cell having that searched value and if column has more than one searched values then cursor should move to next cell having that particular searched value and so on .e.g
----------
column1
----------
ANATR
ANTON
AROUT
BERGS
AROUT
BLONP
AROUT
BONAP
---------
suppose that if we search for value AROUT which is in cell nomber 3,5 and 7 when user hit the serch button first time then data grid cursor should move to the 3rd cell and when user again click search button then datagrid cursor should move to the 5th cell and when user click search button third time then datagrid cursor should move to the 7th cell
i have tried many combination with FOR loop and IF condition
but with following code by default FOR loop starts from first row and goes till the 7th row but does not comes back to the first row,and when we click the search button after clicking the 6th row it does not find previous two values which are in 3rd and 5th row''
how can i loop back to the first row.
it is hard to explain for me but i hope that i am able to convey my point
my code is

Dim j As Integer
Dim i As Integer
j = DataGridView1.Rows.Count - 2
i = DataGridView1.CurrentRow.Index + 1
For i = i To j
    If DataGridView1.Rows(i).Cells(3).Value.ToString.ToLower = TextBox4.Text.ToLower Then
        DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(3)
        Exit For
    End If
Next



<编辑>取出主题的所有大写字母,请不要大喊大叫.固定代码标记.</Edit;>



<Edit>Took out all caps in subject, please don''t shout. Fixed code tags.</Edit;>

推荐答案

尝试一下.你很亲密只需将循环索引调整为绕圈"即可.


Try this. You were close; just needed to adjust the looping indices to "circle around."


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim jMax As Integer, iCurr As Integer
    Dim iLoop As Integer, idx As Integer

    jMax = DataGridView1.Rows.Count - 2
    iCurr = DataGridView1.CurrentRow.Index + 1

    For iLoop = iCurr To jMax + iCurr
        idx = iLoop Mod (jMax + 1)
        If DataGridView1.Rows(idx).Cells(3).Value.ToString.ToLower = TextBox4.Text.ToLower Then
            DataGridView1.CurrentCell = DataGridView1.Rows(idx).Cells(3)
            Exit For
        End If
    Next
End Sub


这篇关于问题循环和遍历列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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