how to bring datatable to remain at the last modified recordset

H

Horst Mayer

Hello everybody,
I update the data so:

dtData1 = GetDataTable(OpenConnection, "select * from nursing where
PatientID = 33 order by ID", objAdapter)
Dim cmdBuilder As New SqlClient.SqlCommandBuilder(objAdapter)
objAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand

With dtData1
.Rows(DataGridView1.CurrentRow.Index).Item("PatientId") =
iPatientID
.Rows(DataGridView1.CurrentRow.Index).Item("Diagnose") = "New
diagnosis"
.Rows(DataGridView1.CurrentRow.Index).Item("Erstellt") = sUserID
End With

objAdapter.Update(dtData1)
dtData1 = GetDataTable(OpenConnection, "select * from nursing where
PatientID = 33 order by ID")
DataGridView1.DataSource = dtData1

Now all controlls (Grid, Textboxes) show to the first record. How to bring
datatable dtData1 to go to the last modified record?

Here is the complete code in the module:

Public Class aaa

Dim dtData1 As New DataTable
Dim iPatientID As Integer
Dim objAdapter As New SqlClient.SqlDataAdapter
Private Sub frmHandlung_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dtData1 = GetDataTable(OpenConnection, "select * from nursing where
PatientID = 33", objAdapter)
Me.DataGridView1.DataSource = dtData1
iPatientID = 33
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
dtData1 = GetDataTable(OpenConnection, "select * from nursing where
PatientID = 33 order by ID", objAdapter)
Dim cmdBuilder As New SqlClient.SqlCommandBuilder(objAdapter)
objAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand

With dtData1
.Rows(DataGridView1.CurrentRow.Index).Item("PatientId") =
iPatientID
.Rows(DataGridView1.CurrentRow.Index).Item("Diagnose") = "New
diagnosis"
.Rows(DataGridView1.CurrentRow.Index).Item("Erstellt") = sUserID
End With

objAdapter.Update(dtData1)
dtData1 = GetDataTable(OpenConnection, "select * from nursing where
PatientID = 33 order by ID")
DataGridView1.DataSource = dtData1
End Sub
Private Function GetDataTable(ByRef objConn As SqlClient.SqlConnection,
ByVal SQLQuery As String, Optional ByRef dataAdapter As
SqlClient.SqlDataAdapter = Nothing) As DataTable
Dim ds As New DataSet, objAdapter As SqlClient.SqlDataAdapter,
objcmd As SqlClient.SqlCommand

If objConn.State = ConnectionState.Open Then
objcmd = New SqlClient.SqlCommand(SQLQuery, objConn)
objAdapter = New SqlClient.SqlDataAdapter()
objAdapter.SelectCommand = objcmd
If Not dataAdapter Is Nothing Then
dataAdapter = objAdapter
End If
objAdapter.Fill(ds)
objConn.Close()
Return ds.Tables(0)
Else
Return Nothing
End If

End Function
End Class

Thank you very much for your ideas!
Horst
 
G

Gregory A. Beamer

Now all controlls (Grid, Textboxes) show to the first record. How to
bring datatable dtData1 to go to the last modified record?

This is under your control. One easy way would be to sort the records on
modified date, but it does not solve all of the potential problems you
might be having.

if you are paging, you will likely be served best by a custom pager where
you can actually filter through until you get to the page with your item on
it.

If this does not answer your question, or at least get you thinking of
ideas, post back and clarify the question.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top