Results 1 to 10 of 10

Thread: [RESOLVED] Increment value by 1 from specified value

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Resolved [RESOLVED] Increment value by 1 from specified value

    Hi,

    In a DGV i want to select rows and then every row that is selected (checked) must be 're-numbered' from a specified value and onwards.

    Meaning....

    Say I have 5 rows selected then I specified where to start (in my code below I say start at 94) Then column 2 must look like this:

    95
    96
    97
    98
    99



    Code:
    Dim lastRow As Integer = 94
    
            For i = lastRow +1  To ?? (maybe I must count the number of checked rows?)
    
                For Each checkedRow As DataGridViewRow In Me.TblRiskAssessmentDataGridView.Rows
                    Dim checked As Boolean = CType(checkedRow.Cells("Column1").Value, Boolean)
                    If checked Then
                        checkedRow.Cells(2).Value = i + 1
                    End If
                Next
            Next
    thanks....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Increment value by 1 from specified value

    lastRow contains the value you want to use but you're not actually using it anywhere. As I have said more than once before, you should be working out the logic first and, if necessary, writing it down or typing it out. You can then compare the code you write to that algorithm to see whether it actually does what you want it to. You seem to have again written code without actually working out what that code is supposed to do. Each time you find a checked row, you need to put the desired value, i.e. lastRow, into that row and then you need to increment that value, i.e. lastRow.

    You're still making the same mistakes as you have been for the last 2 and a half years and I can't really be bothered repeating myself any more. Looks like you will be rid of the garbage and the gold. I'm done.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Increment value by 1 from specified value

    Looks like you will be rid of the garbage and the gold
    Sorry if I hurt your feelings, John....

    I'm done
    Maybe that is for the best. Because honestly I am actually too afraid to ask questions here because of your tantrums... Always moan and complain about everything where in actual fact you did not need to respond at all if the question is not to your high standards. It seems that there is this notion on vbforums that the person asking a question is always wrong, and the person choosing to answer is always right - and just by responding automatically gains the right to lambaste the person asking a question.

    But as I said if I don't get a straight up answer......


    PS:

    Each time you find a checked row, you need to put the desired value, i.e. lastRow, into that row and then you need to increment that value, i.e. lastRow.
    For what is worth you are wrong because every row will have the same value then...
    Last edited by schoemr; Nov 5th, 2019 at 08:34 AM.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Increment value by 1 from specified value

    It looks like your approach is wrong, you don't need to know how many rows were checked, only the starting number.

    Code:
    Dim lastRow As Integer = 94
    
            
                For Each checkedRow As DataGridViewRow In Me.TblRiskAssessmentDataGridView.Rows
                    Dim checked As Boolean = CType(checkedRow.Cells("Column1").Value, Boolean)
                    If checked Then
                        lastrow += 1 
                        checkedRow.Cells(2).Value = lastrow
                        
                    End If
                Next
    This code isn't tested
    Last edited by wes4dbt; Nov 5th, 2019 at 06:07 PM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Increment value by 1 from specified value

    Quote Originally Posted by wes4dbt View Post
    It looks like your approach is wrong, you don't need to know how many rows were checked, only the starting number.

    Code:
    Dim lastRow As Integer = 94
    
            
                For Each checkedRow As DataGridViewRow In Me.TblRiskAssessmentDataGridView.Rows
                    Dim checked As Boolean = CType(checkedRow.Cells("Column1").Value, Boolean)
                    If checked Then
                        lastrow += 1 
                        checkedRow.Cells(2).Value = lastrow
                        
                    End If
                Next
    This code isn't tested
    Hi Wes,

    Thank you very much for this. It is working perfectly. Yesterday when I was trying to figure it out I actually reached this at some point:
    Code:
    checkedRow.Cells(2).Value = lastRow += 1
    wrong, but I think close

    This never occurred to me:

    Code:
    lastrow += 1 
      checkedRow.Cells(2).Value = lastrow
    Thanks again...

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: [RESOLVED] Increment value by 1 from specified value

    It was probably closer than you think...

    Code:
    checkedRow.Cells(2).Value = lastRow += 1
    Code:
    checkedRow.Cells(2).Value = lastRow + 1
    += is an assignment and doesn't return a value. You could write...

    Code:
    Dim value as integer = 9
    value += 1
    Which is equivalent to...

    Code:
    Dim value as integer = 9
    value = value + 1

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Increment value by 1 from specified value

    Quote Originally Posted by schoemr View Post
    ...
    This never occurred to me:

    Code:
    lastrow += 1 
      checkedRow.Cells(2).Value = lastrow
    ...
    I was kind of mystified by that yesterday.
    jmc noted it in his post.
    Quote Originally Posted by jmcilhinney View Post
    ... Each time you find a checked row, you need to put the desired value, i.e. lastRow, into that row and then you need to increment that value, i.e. lastRow.
    ...
    And you even quoted it in your response, but assumed it wouldn't work for some reason, which was the part that mystified me.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  8. #8
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: [RESOLVED] Increment value by 1 from specified value

    I think every programmer has a blind spot now and then.... can't see the solution even if it bit ya in the ass
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Increment value by 1 from specified value

    Quote Originally Posted by passel View Post
    I was kind of mystified by that yesterday.
    jmc noted it in his post.

    And you even quoted it in your response, but assumed it wouldn't work for some reason, which was the part that mystified me.
    Hi Passel,

    This is what I understood at that point in time.

    Each time you find a checked row, you need to put the desired value, i.e. lastRow, into that row
    Code:
    checkedRow.Cells(2).Value = lastrow
    then you need to increment that value
    Code:
    checkedRow.Cells(2).Value = lastrow + 1

    That is why I said every row will have the same number then.

    A simple "increment means +=" did not occur to me at that time (on the 5th November) but by the next day on the 6th November (my #6) I said that I got to that part (the +=) I just then needed the correct way of implementing it.

    But Passel thank you for your contribution.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: [RESOLVED] Increment value by 1 from specified value

    Quote Originally Posted by .paul. View Post
    It was probably closer than you think...

    Code:
    checkedRow.Cells(2).Value = lastRow += 1
    Code:
    checkedRow.Cells(2).Value = lastRow + 1
    += is an assignment and doesn't return a value. You could write...

    Code:
    Dim value as integer = 9
    value += 1
    Which is equivalent to...

    Code:
    Dim value as integer = 9
    value = value + 1
    Hi Paul, thank you very much for your time and effort to explain it to me. I much appreciate it

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