Results 1 to 4 of 4

Thread: Increment cell data on search

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    17

    Increment cell data on search

    I have a datagridview on which I perform a search (for a keyword). The rows in which the keyword exists are highlighted and a checkbox in a DGV column is checked. What I need to do is is to add a reference to each of the highlighted rows in Col2 which is based on a user entered format and incremented by 1. For example, the format might be "PRG_CR123_x" where x is the incremented number. I'm sort of succeeding BUT what I actually get is the row number NOT the row number of the search. In the screenshot you can see that the first row in the search is row 18 but the 'TC' col needs to show "PRG_1" and so on.Name:  Increment.jpg
Views: 347
Size:  24.0 KB

    Here's my code so far.
    Thanks
    David

    Private Sub cmdSearch_Click(sender As System.Object, e As System.EventArgs) Handles cmdSearch.Click
    Dim i As Integer = 0
    Dim Inc As String = txtCurFormat.Text

    For Each row As DataGridViewRow In DataGridView1.Rows
    Dim KeyWord As String = row.Cells(1).Value.ToString
    If KeyWord.Contains("Then") Then
    'row.DefaultCellStyle.BackColor = Color.Red
    row.Selected = True
    row.Cells(3).Value = True
    row.Cells(2).Value = inc & i
    End If
    i = i + 1
    Next

    End Sub

  2. #2
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Increment cell data on search

    First use code tags or highlight the code and press the VE button in the forum editor toolbar. This will keep your indented VB formatting and make your post easier to understand.

    The problem is simple. You need to put your i = i + 1 inside your if statement.

    As it is currently outside the if statement it is increasing on every pass in the loop.

    If you want it to start at one then initialize your i variable to 1 instead of 0.

    Honestly if you had stepped through your code in debug mode you would have seen the problem and not needed to post this question.

    VB Code:
    1. Private Sub cmdSearch_Click(sender As System.Object, e As System.EventArgs) Handles cmdSearch.Click
    2.     Dim Inc As String = txtCurFormat.Text
    3.     Dim i As Integer = 1
    4.  
    5.     For Each row As DataGridViewRow In DataGridView1.Rows
    6.       Dim KeyWord As String = row.Cells(1).Value.ToString
    7.       If KeyWord.Contains("Then") Then
    8.         'row.DefaultCellStyle.BackColor = Color.Red
    9.         row.Selected = True
    10.         row.Cells(3).Value = True
    11.         row.Cells(2).Value = Inc & i
    12.         i = i + 1
    13.       End If
    14.     Next
    15.   End Sub
    Last edited by Gruff; Dec 30th, 2014 at 12:37 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    17

    Re: Increment cell data on search

    Quote Originally Posted by Gruff View Post
    First use code tags or highlight the code and press the VE button in the forum editor toolbar. This will keep your indented VB formatting and make your post easier to understand.

    The problem is simple. You need to put your i = i + 1 inside your if statement.

    As it is currently outside the if statement it is increasing on every pass in the loop.

    If you want it to start at one then initialize your i variable to 1 instead of 0.

    Honestly if you had stepped through your code in debug mode you would have seen the problem and not needed to post this question.

    VB Code:
    1. Private Sub cmdSearch_Click(sender As System.Object, e As System.EventArgs) Handles cmdSearch.Click
    2.     Dim Inc As String = txtCurFormat.Text
    3.     Dim i As Integer = 1
    4.  
    5.     For Each row As DataGridViewRow In DataGridView1.Rows
    6.       Dim KeyWord As String = row.Cells(1).Value.ToString
    7.       If KeyWord.Contains("Then") Then
    8.         'row.DefaultCellStyle.BackColor = Color.Red
    9.         row.Selected = True
    10.         row.Cells(3).Value = True
    11.         row.Cells(2).Value = Inc & i
    12.         i = i + 1
    13.       End If
    14.     Next
    15.   End Sub
    Thank you Gruff - apologies ..I wasn't aware of the VE button but will use from now on.

    Sorry for wasting forum member's time...clearly we're not all geniuses!!!

    Job done though.

    David

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Increment cell data on search

    Debugging is the first step all the time. If you aren't familiar with breakpoints and stepping, play with that, because they are the most valuable tools at your disposal. Far more than this forum, or even the whole internet. Without those tools, you are staring at the code guessing at what it is doing. With those tools, you can watch every step of the way. You can see the sausage being made.

    By the way, the # tag also does some code formatting. Which one you use is a matter of preference. I find that # is nicer in many cases because it makes copying and pasting easier. The VE tag (which is really VB, with the right side missing) makes for a prettier display, but a copying pain. Your choice.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width