hi im having errors with listview binding navigation specifically in last >> and first <<

here's my code

Dim inc As Integer
    Dim MaxRows As Integer
    Dim dr As DataRow
    Dim ds As DataSet

Public Sub navigaterecords()
        txtMed_ID.Text = ListView1.Items(inc).SubItems(0).Text
        txtMed_name.Text = ListView1.Items(inc).SubItems(1).Text
        txtfname.Text = ListView1.Items(inc).SubItems(2).Text
        txtMname.Text = ListView1.Items(inc).SubItems(3).Text
        txtDept_ID.Text = ListView1.Items(inc).SubItems(4).Text
        cboxgender.Text = ListView1.Items(inc).SubItems(5).Text
        txtDOB.Text = ListView1.Items(inc).SubItems(6).Text
        txtMed_Type.Text = ListView1.Items(inc).SubItems(7).Text
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        If inc <> 0 Then
            inc = 0
            navigaterecords()
        End If
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        If inc <> MaxRows - 1 Then
            inc = MaxRows - 1
            navigaterecords()
        End If
    End Sub

any help i will appreciate! thanks :)
God bless

your procedure navigaterecords() not have a parameter..
so when u call it, your procedure didn't get new value of inc variable.

so,, add the parameter on your procedure :

Public Sub navigaterecords(ByVal inc as Integer)
...
End sub

when call :

If inc <> 0 Then
    inc = 0
    navigaterecords(Inc)
End If

thanks i tried ur codes but argumentoutofrangeexception was unhandled

InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index

i think it cause your checking index was wrong...

can u show an error in which line?

actualy,,i just added a parameter, it cause your procedure never get the value (inc) updated when u checking the inc or maxrow. so, an error depend on your codes.
so where the error exactly? which line of your codes?

there in navigatate()

txtMed_ID.Text = ListView1.Items(inc).SubItems(0).Text

See if this helps.

Public Sub navigaterecords(ByVal lvItemIndex As Integer)
        With ListView1.Items(lvItemIndex)
            MsgBox(.SubItems(0).Text) '// for testing.
        End With
    End Sub
    '// First.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        navigaterecords(0)
    End Sub
    '// Last.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        navigaterecords(ListView1.Items.Count - 1)
    End Sub

hi thanks codeorder! i tried ur code it worked well.. but how if i click btn first and it will appear in the textboxes? because it doesnt appear

Public Sub navigaterecords(ByVal lvItemIndex As Integer)
        With ListView1.Items(lvItemIndex)
            txtMed_ID.Text = .SubItems(0).Text
            txtMed_name.Text = .SubItems(1).Text
            txtfname.Text = .SubItems(2).Text
            txtMname.Text = .SubItems(3).Text
            txtDept_ID.Text = .SubItems(4).Text
            cboxgender.Text = .SubItems(5).Text
            txtDOB.Text = .SubItems(6).Text
            txtMed_Type.Text = .SubItems(7).Text
        End With
    End Sub

thanks codeorder! it worked well friend! :)

but im gonna ask again if u dont mind..

i also have next and previous button

there's an error when i reached the last record

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

        If inc <> MaxRows - 1 Then
            inc = inc + 1
            navigaterecords(inc)
        Else
            MsgBox("No More Rows")
        End If

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        If inc > 0 Then
            inc = inc - 1
            navigaterecords(inc)
        ElseIf inc = -1 Then
            MsgBox("No Records Yet")
        ElseIf inc = 0 Then
            MsgBox("First Record")
        End If

    End Sub

says outofindexrange "21" not part of index

For this to work, you need to have ListView1.MultiSelect = False.

'// btnPrevious.
        If Not ListView1.SelectedItems.Count = 0 Then '// check if item is selected.
            With ListView1.SelectedItems(0)
                If Not .Index = 0 Then '// if not current index ='s the first item.
                    ListView1.Items(.Index - 1).Selected = True '// select previous item.
                    ListView1.Items(.Index - 1).EnsureVisible() '// make sure selected item is visible if ListView has vertical scrollbar.
                    navigaterecords(.Index - 1) '// get records for previous item.
                Else
                    MsgBox("Index at Start of records.")
                End If
            End With
        End If
'// btnNext.
        If Not ListView1.SelectedItems.Count = 0 Then '// check if item is selected.
            With ListView1.SelectedItems(0)
                If Not .Index = ListView1.Items.Count - 1 Then '// if not current index ='s the last item.
                    ListView1.Items(.Index + 1).Selected = True '// select next item.
                    ListView1.Items(.Index + 1).EnsureVisible() '// make sure selected item is visible if ListView has vertical scrollbar.
                    navigaterecords(.Index + 1) '// get records for next item.
                Else
                    MsgBox("Index at End of records.")
                End If
            End With
        End If

If .MultiSelect is a necessity, you can always toggle it to False just before running the code in each Button and set it back to True when done.

ListView1.MultiSelect = False
        '// code here for Previous/Next.
        ListView1.MultiSelect = True

thank you codeoder this thread is solved :)
GOD bless u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.