[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cycle through dataset

Posted on 2006-11-09
9
Medium Priority
?
603 Views
Last Modified: 2010-08-05
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







0
Comment
Question by:gogetsome
  • 4
  • 4
9 Comments
 
LVL 39

Expert Comment

by:appari
ID: 17912183
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
0
 

Author Comment

by:gogetsome
ID: 17912269
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.
0
 
LVL 39

Expert Comment

by:appari
ID: 17912270
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gogetsome
ID: 17912305
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?
0
 

Author Comment

by:gogetsome
ID: 17912331
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.
0
 
LVL 39

Expert Comment

by:appari
ID: 17912362
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
0
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 17912367
for first and last records

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        Try
            With cboTitle

                curRowIndex = 0

             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 btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        Try
            With cboTitle

                curRowIndex =  dsLifeIssues.Tables(0).Rows.Count - 1

             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

0
 
LVL 3

Expert Comment

by:vsvb
ID: 17913672
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
0
 

Author Comment

by:gogetsome
ID: 17916956
Thanks for your efforts Appari. It know works.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question