Navigate in a System.Data.Datatable

  • Thread starter Thread starter Lorenzo Soncini
  • Start date Start date
L

Lorenzo Soncini

Goodmorning folks,
I have one little problem. Can I navigate through a row of DataTable ?

I need to move to first,next,previus,last record of the datatable.

Whit the Rowcollection I read the data but I don't ave an order of
"navigation"

Sorry for my english, and good work
Thanks for any idea
Lorenzo Soncini
 
Normally, I just use something like the following:

For Each MyRow As DataRow in MyDataTable.Rows
Debug.WriteLine(MyRow("MyFieldOne") & ", " &
MyRow("MyFieldTwo"))
Next

-Kevin Buchan
 
hi
If you specifically want to move to a Particular Record
like Next, Previous, Last, First then u need to write a
code snippet which constitutes of some methods with our
own logic.

I can provide you the code if you don't have.
thanks
srinivas moorthy
 
Hi Friend

Here is the Code Snippet for You. Just Change the
Connection string and input parameters to your environment.

Just copy paste the Snippet in a Vb Form of VB.Net and
change the Connection String and input parameters

Imports System.Data.SqlClient
Public Class RecordNavigation
Inherits System.Windows.Forms.Form

#Region "InitializeEnvironment Variables"

''''''''''''''''' Initialize the Environment
Variables '''''''''''''''''''''''''''''''''''''''''''''''''
''''''

Public I As Integer
Public SqlCon As New SqlConnection("Data Source=srins;
Initial Catalog=VET; User ID=sa; Password=007;")
Public dataTable As New dataTable()
Public dataSet As New dataSet()
Public DR As DataRow
Public RowCount As Integer

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
#End Region

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btn_Next As
System.Windows.Forms.Button
Friend WithEvents btn_Previous As
System.Windows.Forms.Button
Friend WithEvents btn_MoveFirst As
System.Windows.Forms.Button
Friend WithEvents btn_MoveLast As
System.Windows.Forms.Button
Friend WithEvents txt_OwnerID As
System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.txt_OwnerID = New System.Windows.Forms.TextBox()
Me.btn_Next = New System.Windows.Forms.Button()
Me.btn_Previous = New System.Windows.Forms.Button()
Me.btn_MoveFirst = New System.Windows.Forms.Button
()
Me.btn_MoveLast = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'txt_OwnerID
'
Me.txt_OwnerID.Location = New System.Drawing.Point
(72, 56)
Me.txt_OwnerID.Name = "txt_OwnerID"
Me.txt_OwnerID.Size = New System.Drawing.Size(160,
20)
Me.txt_OwnerID.TabIndex = 0
Me.txt_OwnerID.Text = ""
'
'btn_Next
'
Me.btn_Next.Location = New System.Drawing.Point
(56, 96)
Me.btn_Next.Name = "btn_Next"
Me.btn_Next.Size = New System.Drawing.Size(72, 24)
Me.btn_Next.TabIndex = 1
Me.btn_Next.Text = "Next >>"
'
'btn_Previous
'
Me.btn_Previous.Location = New System.Drawing.Point
(152, 96)
Me.btn_Previous.Name = "btn_Previous"
Me.btn_Previous.Size = New System.Drawing.Size(80,
24)
Me.btn_Previous.TabIndex = 2
Me.btn_Previous.Text = "<< Previous "
'
'btn_MoveFirst
'
Me.btn_MoveFirst.Location = New
System.Drawing.Point(56, 128)
Me.btn_MoveFirst.Name = "btn_MoveFirst"
Me.btn_MoveFirst.Size = New System.Drawing.Size
(72, 24)
Me.btn_MoveFirst.TabIndex = 3
Me.btn_MoveFirst.Text = "Move First"
'
'btn_MoveLast
'
Me.btn_MoveLast.Location = New System.Drawing.Point
(152, 128)
Me.btn_MoveLast.Name = "btn_MoveLast"
Me.btn_MoveLast.Size = New System.Drawing.Size(80,
24)
Me.btn_MoveLast.TabIndex = 4
Me.btn_MoveLast.Text = "Move Last"
'
'RecordNavigation
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(392, 253)
Me.Controls.AddRange(New
System.Windows.Forms.Control() {Me.btn_MoveLast,
Me.btn_MoveFirst, Me.btn_Previous, Me.btn_Next,
Me.txt_OwnerID})
Me.Name = "RecordNavigation"
Me.Text = "RecordNavigation"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub RecordNavigation_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
' just get the DataAdapter
Try
I = 0
Dim oComd As New SqlCommand()
oComd.CommandText = "TestProcedure "
oComd.CommandType = CommandType.StoredProcedure
oComd.Parameters.Add("@OwnerID",
Convert.ToInt64(304442788964445))
oComd.Connection = SqlCon


Dim SqlAdapter As New SqlDataAdapter(oComd)
SqlAdapter.Fill(dataTable)

DR = dataTable.Rows(I)
txt_OwnerID.Text = DR(0)

RowCount = dataTable.Rows.Count
Catch ee As SqlException
MessageBox.Show(ee.ToString())

End Try

End Sub



Private Sub btn_Next_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btn_Next.Click

If I = RowCount - 1 Then Return

I = I + 1

DR = dataTable.Rows(I)
txt_OwnerID.Text = DR(0)


End Sub

Private Sub btn_Previous_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btn_Previous.Click
If I = -1 Or I = 0 Then
I = 0
Return
End If

I = I - 1
DR = dataTable.Rows(I)
txt_OwnerID.Text = DR(0)

End Sub

Private Sub btn_MoveFirst_Click(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
btn_MoveFirst.Click
I = 0

DR = dataTable.Rows(I)
txt_OwnerID.Text = DR(0)

End Sub

Private Sub btn_MoveLast_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btn_MoveLast.Click

I = RowCount - 1

DR = dataTable.Rows(I)
txt_OwnerID.Text = DR(0)

End Sub

End Class

Thanks

srinivas moorthy
 
There is no concept of navigation in the traditional 'resultset' way with
ADO.NET, kinda (you'll see why I say kinda in a second).

A DataTable has a property called Rows. This is a collection of DataRow
objects which you can walk through much like an array. If you maintain an
index into that collection in code yourself, you can update the index
manually and retrieve the desired row as you see fit.

Now for the kinda. if you are doing databinding on a form, there is
something wonderful called a CurrencyManager. This gives you the pointer you
need into DataTable.Rows to find out which row is the current one, if indeed
there is one. Take a look at the online help for how CurrencyManager works.

Hope that helps,

--
Peter Wright
Author of ADO.NET Novice To Pro, from Apress Inc.


_____________________________
 
Back
Top