-
Aug 29th, 2017, 07:09 AM
#1
Thread Starter
Hyperactive Member
[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 function
-
Aug 29th, 2017, 07:15 AM
#2
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.
-
Aug 29th, 2017, 07:38 AM
#3
Thread Starter
Hyperactive Member
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
-
Aug 29th, 2017, 08:11 AM
#4
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.
-
Aug 29th, 2017, 08:23 PM
#5
Thread Starter
Hyperactive Member
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
-
Aug 29th, 2017, 08:46 PM
#6
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:
Dim rows = myDataGridView.Rows. Cast(Of DataGridViewRow)(). OrderByDescending(Function(row) CInt(row.Cells(0).Value)). ToArray() For i = 0 To rows.GetUpperBound(0) rows(i).Cells(1).Value = i + 1 Next
-
Sep 3rd, 2017, 08:44 AM
#7
Thread Starter
Hyperactive Member
Re: Finding position in datagrid view
Originally Posted by jmcilhinney
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:
Dim rows = myDataGridView.Rows. Cast(Of DataGridViewRow)(). OrderByDescending(Function(row) CInt(row.Cells(0).Value)). ToArray() For i = 0 To rows.GetUpperBound(0) rows(i).Cells(1).Value = i + 1 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
-
Sep 3rd, 2017, 10:09 AM
#8
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.
-
Sep 3rd, 2017, 02:13 PM
#9
Thread Starter
Hyperactive Member
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
-
Sep 3rd, 2017, 06:02 PM
#10
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.
-
Sep 4th, 2017, 09:40 AM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] Finding position in datagrid view
please help me. this is frustrating me the more
-
Sep 5th, 2017, 01:32 AM
#12
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.
-
Jan 19th, 2018, 08:37 PM
#13
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|