Dataset / Datatable in Visual Developer 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravno
    New Member
    • Apr 2008
    • 14

    Dataset / Datatable in Visual Developer 2008

    I am developing a Windows-application - database mssql - visual basic.
    I add Datasets to forms, bind them to daatagridview/textboxes - fill with dataadapter - everything's fine so far.

    Here's my question:

    how do I programmaticaly move from a record/datarow i a dataset to another.
    I know the value of the key-field and want that record to be shown.
    If I use the dataset.datatab le.findbykey-method, the program finds the record, but it doesn't move there.
    Anybody with good ideas ?

    ravno
    Last edited by Frinavale; Jan 19 '09, 05:17 PM. Reason: Moved to VB.NET Answers from .NET
  • OuTCasT
    Contributor
    • Jan 2008
    • 374

    #2
    You have to use a currency manager to navigate through the datasets rows.

    Code:
     
    'Commands and adapter for the Employee masterfile
    Dim sqlEmployeeCommand As SqlCommand
    Dim sqlEmployeeAdapter As SqlDataAdapter
    Dim sqlEmployeeDataTable As DataTable
    'Currency Managers for Commands above
    Dim sqlEmployeeManager As CurrencyManager
     
    'lblPaymentPeriod.Text = strPaymentPeriod
    sqlEmployeeCommand = New SqlCommand("SELECT * FROM EmployeeDetails order by EmployeeID", sqlcon)
    sqlEmployeeAdapter = New SqlDataAdapter(sqlEmployeeCommand)
    sqlEmployeeDataTable = New DataTable
    sqlEmployeeAdapter.Fill(sqlEmployeeDataTable)
    ' Fill Table with EmployeeDetails
    lblPaymentPeriod.DataBindings.Add("text", sqlEmployeeDataTable, "EmployeePaymentType", True)
     
    sqlEmployeeManager = DirectCast(Me.BindingContext(sqlEmployeeDataTable), CurrencyManager)
    When you want to navigate forward :
    Code:
    sqlEmployeeManager.Position += 1
    Backwards:
    Code:
     
    sqlEmployeeManager.Position -= 1
    [SIZE=2]
    [/SIZE]

    Comment

    • ravno
      New Member
      • Apr 2008
      • 14

      #3
      Thank you for the answer.

      It looks as the same functionality, I get from:
      databindingsour ce.movenext() or databindingsour ce.movelast()

      What I really want is
      "move to the record, where Key=something"

      In Access2000 I did it this way:
      Dim rs As DAO.Recordset
      Set rs = Me.Recordset.Cl one
      rs.FindFirst "MemberId = " & someValue
      Me.Bookmark = rs.Bookmark
      rs.Close

      All in all I still have some trouble working with datasets programmaticall y.
      How do I eg adress values in the 'current record' in a dataset.datatab le ?

      I have a form with a dataset - showed in a datagrid. I can use the grid to navigate the recordset, but how do I access the dataset, when I need a value stored in the dataset ?
      In Access I do it like this:
      value=recordset .fields!fieldna me or
      value=me.record set.fieldname

      The search-problem above could be described:

      dataset.movefir st
      while dataset.current record.keyvalue <>mykeyvalue and not eof
      dataset.movenex t
      end while
      if not eof then
      doSomeThing(dat aset.currentrec ord.keyvalue)
      end if

      ravno
      Last edited by ravno; Feb 2 '09, 09:25 AM. Reason: posted before finished writing /ravno

      Comment

      • OuTCasT
        Contributor
        • Jan 2008
        • 374

        #4
        So you want to search the dataset for a specific entry correct. i added a toolstriptextbo x where they can type in the specific employeecode so they can retrieve the information for that code.

        [SIZE=2][/SIZE]
        Code:
         
        If ToolStripTextBox1.Text = "" Then Exit Sub
        Dim SavedRow As Integer = sqlEmployeeManager.Position
        Dim FoundRows() As DataRow
        sqlEmployeeDataTable.DefaultView.Sort = "EmployeeID"
        FoundRows = sqlEmployeeDataTable.Select("EmployeeID LIKE '" + ToolStripTextBox1.Text + "*'")
        If FoundRows.Length = 0 Then
        sqlEmployeeManager.Position = SavedRow
        Else
        sqlEmployeeManager.Position = sqlEmployeeDataTable.DefaultView.Find(FoundRows(0).Item("EmployeeID"))
        End If

        Comment

        • ravno
          New Member
          • Apr 2008
          • 14

          #5
          Hi OuTCasT
          I didn't make it work - but your use of 'position' really made something:

          In my form I have
          - dataset DS with a datatable DT
          - a bindingsource BS, where
          BS.DataSource=D S
          BS.DataMember=D T
          - a tableAdapter and a tableadapterman ager

          - a datagridview with DataSource=BS

          The command
          BS.Position=BS. Find("KeyName", keyvalue)
          moves focus/currentrecord in the dataset - and the datagridview - to the wanted row/record.

          DS.DT.Rows(BS.P osition).Item(" ColumnName") let me acces the data.

          Don't know, if it's the 'authorized' way to do things, but it works for me.
          Thanks
          ravno

          Comment

          • OuTCasT
            Contributor
            • Jan 2008
            • 374

            #6
            At least you got it working and you can access the desired information. :)

            Comment

            Working...