Results 1 to 13 of 13

Thread: [RESOLVED] Finding position in datagrid view

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Resolved [RESOLVED] Finding position in datagrid view

    I want to find positions of values on a given column on a datagrid view table. i have tried adding this code to the rowsAdded event but still giving me tough time
    dim i as integer
    i=e.index()
    function positio()
    'dont know what to add here
    datagrid.rows(i).cell(2).value=formula for calculation
    end functionName:  position_.jpg
Views: 191
Size:  73.9 KB

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

    Re: Finding position in datagrid view

    First of all, when do you want to find these rows? The RowsAdded seems an odd choice.

    As for finding what you want, it should be fairly clear that you need an If statement to perform an action if two expressions match.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: Finding position in datagrid view

    I want the position found as soon as a row is added. for the first row the position will be 1. when another row is added, if the value on the second row is greater than the first one it will then change the first row to 2 because only two rows that are present then 1 on the second row

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Finding position in datagrid view

    Hi

    you can use select Case

    here a sample that takes a Value from cell 2
    Code:
     For Each row As DataGridViewRow In DataGridView1.Rows
    
    
                Select Case CInt(row.Cells(2).Value)
                    Case Is = 1
                        row.Cells(2).Style.BackColor = Color.Gold
                    Case Is = 2
                        row.Cells(2).Style.BackColor = Color.Silver
                    Case Is = 3
                        row.Cells(2).Style.BackColor = Color.Brown
                    Case Else
    
    
                End Select
            Next
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: Finding position in datagrid view

    i have tried collecting all the values on column 2 to an arraylist as soon as they are entered and then sorting them to determine the highest then searching for a first match from the sorted arraylist and finding a match from the datagrid view then placing it in the corresponding row but this seems cumbersome

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

    Re: Finding position in datagrid view

    What are you actually trying to achieve here? Are you basically trying to populate the Position column based on the Score column in descending order? If so then the simplest option is probably to actually sort the rows by descending Score, then loop through them and populate the Position, e.g.
    vb.net Code:
    1. Dim rows = myDataGridView.Rows.
    2.                           Cast(Of DataGridViewRow)().
    3.                           OrderByDescending(Function(row) CInt(row.Cells(0).Value)).
    4.                           ToArray()
    5.  
    6. For i = 0 To rows.GetUpperBound(0)
    7.     rows(i).Cells(1).Value = i + 1
    8. Next

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: Finding position in datagrid view

    Quote Originally Posted by jmcilhinney View Post
    What are you actually trying to achieve here? Are you basically trying to populate the Position column based on the Score column in descending order? If so then the simplest option is probably to actually sort the rows by descending Score, then loop through them and populate the Position, e.g.
    vb.net Code:
    1. Dim rows = myDataGridView.Rows.
    2.                           Cast(Of DataGridViewRow)().
    3.                           OrderByDescending(Function(row) CInt(row.Cells(0).Value)).
    4.                           ToArray()
    5.  
    6. For i = 0 To rows.GetUpperBound(0)
    7.     rows(i).Cells(1).Value = i + 1
    8. Next
    you wrote this code for me and it worked but i don't know how it works(ie explanation). can you please explain the code to me? and also when i ran the code when two records have the same score the program dont differentiate rather it assigns the second one entered another position

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

    Re: [RESOLVED] Finding position in datagrid view

    The first line creates an array containing the rows from the grid in descending order by the Integer value in the column with index 0. The loop then goes through those rows in that order and assigns sequential values, starting at 1, to the column with index 1.

    If you want to assign the same position to rows with the same score then you need to implement some logic to do that. Rows with the same score will always be adjacent to each other in the ordered array so it's not hard to do. You can simply store the last score and the last position in variables and then compare the current score to the last. If they are the same then you would assign the last position to the current row too, otherwise assign a position based on the loop counter and update both variables. I'm not going to write that code for you.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: [RESOLVED] Finding position in datagrid view

    please i wrote this code but anytime i run it and i enter some values into it, it throws an exception "Index was outside the bounds of the array"
    Code:
      Dim rows = dgvScoreSheetTable.Rows.
                              Cast(Of DataGridViewRow)().
                              OrderByDescending(Function(row) CInt(row.Cells(0).Value)).ToArray()
    
            For i = 0 To rows.GetUpperBound(0)
                If dgvScoreSheetTable.RowCount > 2 Then
                    Dim last_total As Integer = CInt(rows(i - 1).Cells(0).Value)
                    Dim lastposition As Integer = CInt(rows(i - 1).Cells1).Value)
    
                    If CInt(rows(i).Cells(0).Value) = last_total Then
                        rows(i).Cells(1).Value = lastposition
                    End If
                Else
                    rows(i).Cells(1).Value = i + 1
                End If
            Next

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

    Re: [RESOLVED] Finding position in datagrid view

    Firstly, whenever you get an IndexOutOfRangeException, the first thing to do should obviously to determine what the index was and what range it was outside.

    Also, the variables I mentioned should be declared OUTSIDE the loop. There's no point retrieving data from the previous row when you already had it on the previous iteration. The variable for the last score would be initialised to something that can't possibly appear in any row, e.g. 0 or -1. That way, you're guaranteed of not matching the first row.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: [RESOLVED] Finding position in datagrid view

    please help me. this is frustrating me the more

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

    Re: [RESOLVED] Finding position in datagrid view

    I already have helped you. If what you actually mean is "write my code for me" then I'm afraid not.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: [RESOLVED] Finding position in datagrid view

    please jmc can you throw more light on the clew you gave to me cause I can't figure the solution out
    Nothing I post is Self Reliable. Use it at your own risk

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