Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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>Took out all caps in subject, please don't shout. Fixed code tags.</Edit;>
Posted
Updated 18-Apr-11 6:26am
v2
Comments
Sandeep Mewara 18-Apr-11 12:14pm    
Looks like a repost!
[no name] 26-Apr-11 15:16pm    
Ok, working code was provided and thanks was received; an extended explanation of the code was also given. Isn't a favorable vote in order?

1 solution

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


VB
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
 
Share this answer
 
Comments
mehdilahori 19-Apr-11 4:38am    
thanks TTM i mean TrustTheMath
thanks thanks thanks
it was really a magic code
please do one more favor
can you explain MOD command
thank you very much
[no name] 19-Apr-11 9:13am    
The MOD command gives you the remainder left after dividing two integers. For example, 12 MOD 5 is 2 -- 5 goes into 12 two times, with a remainder of 2. To cycle through a datagridview column having R rows, starting at row X, one starts at X and processes R rows. However, we get to the end of the column (after processing R - X rows), we need to start again from the beginning. Using MOD allows us to "offset" the current row number back to the rows that were ahead of our starting point.

This may be a clearer explanation:
Let's say we have a 10-row grid (jMax=10, row numbers 0-9), and we've found an item at row 5 (iCurr=5). To find the next one, we'll have to search the 10 rows. So the iLoop above starts at our next row (CurrentRow.Index + 1 = 6), and loops 10 times -- i.e., from iCurr to (jMax + iCurr). However, after 4 rows (6,7,8,9) our index is at 10 and if we were to continue we'd reference non-existent row #10 and generate an error (remember, there are only rows 0-9). This is where the MOD comes in. By taking the remainder of dividing the iLoop value (row number) by jMax, once the iLoop value hits 10, the MOD returns 0. Then when iLoop hits 11, MOD returns 1; at iLoop=12, MOD returns 2, and so forth. Once iLoop hits the end (jMax + iCurr = 15), MOD returns 5 -- the last number we need to check. At this point, we checked rows 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 and are finished.

Hope this helps. How about entering a vote for my solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900