Link to home
Start Free TrialLog in
Avatar of gogetsome
gogetsomeFlag for United States of America

asked on

cycle through dataset

Hello, I have a dataset populated from a dataadapter (code below) the dataset could have any amount of rows from 1-10.
I want to use four buttons: first, next, previous, last to cycle/iterate through the dataset; going foward and backward... You get the idea. Like paging with an asp.net formview or gridview.

How can this be accomplished with the click events of the four buttons?

Here is my code:

Private Sub fillLifeIssuesCB()
        Dim theSQL As String
        Dim DA As OleDb.OleDbDataAdapter
        Dim dsTitle As DataSet
        theSQL = "SELECT DISTINCT ltrim(Title) as Title from LifeIssues"
        DA = New OleDb.OleDbDataAdapter(theSQL, CN)
        dsTitle = New DataSet
        DA.Fill(dsTitle)

        cboTitle.DataSource = dsTitle.Tables(0).DefaultView
        cboTitle.DisplayMember = dsTitle.Tables(0).Columns("Title").ToString

    End Sub

    Private Sub cboTitle_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTitle.SelectedValueChanged
        Dim title As String = cboTitle.SelectedValue.Item("Title")

        Dim theSql As String
        Dim DA As OleDb.OleDbDataAdapter
        Dim dsLifeIssues As DataSet
        Dim strTitle As String = title
        theSql = "SELECT ID, Title, Question, Commentary, Verse, Passage FROM LifeIssues WHERE title = '" & strTitle & "'"
        DA = New OleDb.OleDbDataAdapter(theSql, CN)
        dsLifeIssues = New DataSet
        DA.Fill(dsLifeIssues)

        lblTitle.Text = dsLifeIssues.Tables(0).Rows(0).Item("Title")
        lblQuestion.Text = dsLifeIssues.Tables(0).Rows(0).Item("Question")
        lblCommentary.Text = dsLifeIssues.Tables(0).Rows(0).Item("Commentary")
        lblVerse.Text = dsLifeIssues.Tables(0).Rows(0).Item("Verse")
        lblPassage.Text = dsLifeIssues.Tables(0).Rows(0).Item("Passage")

    End Sub







Avatar of appari
appari
Flag of India image

try these

 Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Try
            With cboTitle
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)
                If myCurrencyManager.Position = 0 Then
                    myCurrencyManager.Position = CType(.DataSource, DataTable).Rows.Count - 1
                Else
                    myCurrencyManager.Position = myCurrencyManager.Position - 1
                End If


            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Try
            With cboTitle
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)

                myCurrencyManager.Position = myCurrencyManager.Position + 1
                If myCurrencyManager.Position = CType(.DataSource, DataTable).Rows.Count - 1 Then
                    myCurrencyManager.Position = 0
                Else
                    myCurrencyManager.Position = myCurrencyManager.Position + 1
                End If


            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub
Avatar of gogetsome

ASKER

Thanks Appari for helping. I'm spoiled with asp.net.
The code you provided is looping through the combobox values and is throwing this error:

unable to cast object of type 'system.data.dataview' to type 'system.data.datatable'.

The value of the combobox is used to filter the select statement for dsLifeIssues dataset. The dsLifeIssues dataset is the one I want to loop through.
small mistake in

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Try
            With cboTitle
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)

                If myCurrencyManager.Position = CType(.DataSource, DataTable).Rows.Count - 1 Then
                    myCurrencyManager.Position = 0
                Else
                    myCurrencyManager.Position = myCurrencyManager.Position + 1
                End If


            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

No that did not do anything. If I comment out the   MsgBox(ex.Message) the code works well to cycle through the combobox's values. BUT, the combobox value is only used to filter (see the WHERE condition above) the data of which populates the second dataset. The second dataset is the one I want to loop through.
For example: if the user selects the abortion value of the combobox that value is used for the dslifesiiues datset to bring back the rows where the title = abortion. It is those records that I want to loop through and not the combobox.
Am I missing something here?
One more though that might help.

If I change
 txtPassage.Text = dsLifeIssues.Tables(0).Rows(0).Item("Passage")

to

 txtPassage.Text = dsLifeIssues.Tables(0).Rows(1).Item("Passage")

where I change the row value to 1 the next abortion row is displayed.
try something like this


      

add a variable at form level

    private curRowIndex as integer = 0

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Try
            With cboTitle
                If curRowIndex = 0 Then
                    curRowIndex = dsLifeIssues.Tables(0).Rows.Count - 1
                Else
                    curRowIndex  = curRowIndex  - 1
                End If

             lblTitle.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Title")
              lblQuestion.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Question")
              lblCommentary.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Commentary")
              lblVerse.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Verse")
              lblPassage.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Passage")

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

   Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Try
            With cboTitle
                If curRowIndex =  dsLifeIssues.Tables(0).Rows.Count - 1 Then
                    curRowIndex = 0
                Else
                    curRowIndex  = curRowIndex  + 1
                End If

             lblTitle.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Title")
              lblQuestion.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Question")
              lblCommentary.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Commentary")
              lblVerse.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Verse")
              lblPassage.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Passage")

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vsvb
vsvb

This is some different approch

1) Declare currentpos variable on your form base
2) suppose you have 4 button first,next,previous,last and call this procedure on each button
   

  Private Sub ButtonFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFirst.Click

        With Me.DataGridView1
            .AutoGenerateColumns = True
            .DataSource = GetDataPos(dt, 1, 10).DefaultView  'here dt = your dataset, (1=first,2=next,3=pre,4=last),    
                                                                                      10=  your row you want to display on each page)

            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
            .BorderStyle = BorderStyle.None
            .EditMode = DataGridViewEditMode.EditOnEnter
            .Refresh()
        End With
    End Sub

3) the procedure is simple its takes your dataset as parameter, your navigation flag, your differnece mostly this is
   static but i wrote it dynamic

Function GetDataPos(ByVal ds As DataSet, ByVal pos As Integer, ByVal diff As Integer) As DataTable
        'pos = 1 as first,2 as next,3 as previous, 4 as last
        Dim dt As DataTable
        dt = ds.Tables(0).Clone
        Dim i As Integer
        Dim startpoint As Integer, endpoint As Integer
        Select Case pos
            Case 1 'first
                startpoint = 0
                endpoint = startpoint + diff
            Case 4 'last
                startpoint = ds.Tables(0).Rows.Count - diff
                endpoint = ds.Tables(0).Rows.Count - 1
            Case 3 'next
                startpoint = currentpos
                endpoint = startpoint + diff
                If endpoint > ds.Tables(0).Rows.Count - 1 Then
                    endpoint = startpoint - ds.Tables(0).Rows.Count - 1
                End If
            Case 2 'previous
                startpoint = startpoint - diff
                endpoint = currentpos
                If startpoint < 0 Then
                    startpoint = 0
                    endpoint = diff
                End If
        End Select
        Dim dr As DataRow
        For i = startpoint To endpoint
            dr = dt.NewRow
            dr.ItemArray = ds.Tables(0).Rows(i).ItemArray
            dt.Rows.Add(dr)
        Next
        Return dt
    End Function

may be you need some testing but you have to test inside this procedure which returns selected rows
and that you directly bind with your datagridview

-best luck
vsvb
Thanks for your efforts Appari. It know works.