Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I tried to select automatically the lowest number of my program w/ it's highlight using listview. but then it always says "There is no position 3" anyone? thanks

VB
Dim ct As Integer
            For ct = 0 To .Tables(0).Rows.Count - 1

            Next


            Dim ls As New ListViewItem
            ls.Text = .Tables(0).Rows(ct).Item(0)
            If ls.SubItems.Add(.Tables(0).Rows(ct).Item(2)) <= ls.SubItems.Add(.Tables(0).Rows(ct).Item(2)) Then
                ls.BackColor = Color.Yellow
                lvpoapproveitem.Items.Add(ls)

            End If
Posted
Updated 18-Oct-13 5:01am
v2

Use For Each Loop Instead of For Loop
See below modified code :
VB
For Each _mRow As DataRow In DataSetName.Tables(0).Rows
     Dim ls As New ListViewItem
     ls.Text = DataSetName.Tables(0).Rows(ct).Item(0)
     If ls.SubItems.Add(_mRow.Item(2)) <= ls.SubItems.Add(_mRow.Item(2)) Then
           ls.BackColor = Color.Yellow
           lvpoapproveitem.Items.Add(ls)
     End If
Next

Also check 3rd column exist or not in your DataSet Table because you are referring Index (2)
VB
.Tables(0).Rows(ct).Item(2)

I hope this will help you. :)
 
Share this answer
 
Perhaps, what you meant to do was have the Next at the bottom of the code, rather than at the top...
That way, ct would change for each row, rather than just being set to the number of rows before you actually use it?
VB
Dim ct As Integer
For ct = 0 To .Tables(0).Rows.Count - 1
    Dim ls As New ListViewItem
    ls.Text = .Tables(0).Rows(ct).Item(0)
    If ls.SubItems.Add(.Tables(0).Rows(ct).Item(2)) <= ls.SubItems.Add(.Tables(0).Rows(ct).Item(2)) Then
        ls.BackColor = Color.Yellow
        lvpoapproveitem.Items.Add(ls)

    End If
Next
 
Share this answer
 

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