Connecting Tech Pros Worldwide Help | Site Map

Dataset / Datatable in Visual Developer 2008

Newbie
 
Join Date: Apr 2008
Location: Denmark
Posts: 14
#1: Jan 19 '09
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.datatable.findbykey-method, the program finds the record, but it doesn't move there.
Anybody with good ideas ?

ravno
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#2: Jan 29 '09

re: Dataset / Datatable in Visual Developer 2008


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

Expand|Select|Wrap|Line Numbers
  1.  
  2. 'Commands and adapter for the Employee masterfile
  3. Dim sqlEmployeeCommand As SqlCommand
  4. Dim sqlEmployeeAdapter As SqlDataAdapter
  5. Dim sqlEmployeeDataTable As DataTable
  6. 'Currency Managers for Commands above
  7. Dim sqlEmployeeManager As CurrencyManager
  8.  
  9. 'lblPaymentPeriod.Text = strPaymentPeriod
  10. sqlEmployeeCommand = New SqlCommand("SELECT * FROM EmployeeDetails order by EmployeeID", sqlcon)
  11. sqlEmployeeAdapter = New SqlDataAdapter(sqlEmployeeCommand)
  12. sqlEmployeeDataTable = New DataTable
  13. sqlEmployeeAdapter.Fill(sqlEmployeeDataTable)
  14. ' Fill Table with EmployeeDetails
  15. lblPaymentPeriod.DataBindings.Add("text", sqlEmployeeDataTable, "EmployeePaymentType", True)
  16.  
  17. sqlEmployeeManager = DirectCast(Me.BindingContext(sqlEmployeeDataTable), CurrencyManager)
  18.  
When you want to navigate forward :
Expand|Select|Wrap|Line Numbers
  1. sqlEmployeeManager.Position += 1
Backwards:
Expand|Select|Wrap|Line Numbers
  1.  
  2. sqlEmployeeManager.Position -= 1
  3.  
[SIZE=2]
[/SIZE]
Newbie
 
Join Date: Apr 2008
Location: Denmark
Posts: 14
#3: Feb 2 '09

re: Dataset / Datatable in Visual Developer 2008


Thank you for the answer.

It looks as the same functionality, I get from:
databindingsource.movenext() or databindingsource.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.Clone
rs.FindFirst "MemberId = " & someValue
Me.Bookmark = rs.Bookmark
rs.Close

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

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!fieldname or
value=me.recordset.fieldname

The search-problem above could be described:

dataset.movefirst
while dataset.currentrecord.keyvalue<>mykeyvalue and not eof
dataset.movenext
end while
if not eof then
doSomeThing(dataset.currentrecord.keyvalue)
end if

ravno
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#4: Feb 2 '09

re: Dataset / Datatable in Visual Developer 2008


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

[SIZE=2][/SIZE]
Expand|Select|Wrap|Line Numbers
  1.  
  2. If ToolStripTextBox1.Text = "" Then Exit Sub
  3. Dim SavedRow As Integer = sqlEmployeeManager.Position
  4. Dim FoundRows() As DataRow
  5. sqlEmployeeDataTable.DefaultView.Sort = "EmployeeID"
  6. FoundRows = sqlEmployeeDataTable.Select("EmployeeID LIKE '" + ToolStripTextBox1.Text + "*'")
  7. If FoundRows.Length = 0 Then
  8. sqlEmployeeManager.Position = SavedRow
  9. Else
  10. sqlEmployeeManager.Position = sqlEmployeeDataTable.DefaultView.Find(FoundRows(0).Item("EmployeeID"))
  11. End If
  12.  
Newbie
 
Join Date: Apr 2008
Location: Denmark
Posts: 14
#5: Feb 3 '09

re: Dataset / Datatable in Visual Developer 2008


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=DS
BS.DataMember=DT
- a tableAdapter and a tableadaptermanager

- 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.Position).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
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#6: Feb 3 '09

re: Dataset / Datatable in Visual Developer 2008


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