Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27193

New records should be appeared on top of datagridview ?

$
0
0
Hi guys, I am working on a project in which datagridview is being populated from MS Access 2007 and users have rights to add new record in MS Access database. So I want that every time when form loads then new records should be appeared on top of datagridview on the basis of Primary Key of Access Database.

Example:
Suppose I have 3 records in my Database A, B, C
A has Primary Key = 1
B has Primary Key = 2
C has Primary Key = 3
So, record C should be appeared on top of datagridview ..

I am using these codes…



Private Sub PendingFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try

'get datareader
Dim cmdString As String = "SELECT * FROM PendingRecords
Dim oSelCmd As OleDbCommand = New OleDbCommand(cmdString, connection)
oSelCmd.CommandType = CommandType.Text
Dim oDr As OleDbDataReader = oSelCmd.ExecuteReader()

''''''''''''''Construct DataTable''''''''''''''
Dim dt As New DataTable
Dim i As Integer
Dim count As Integer = oDr.FieldCount - 1

''''''''''''''Add columns''''''''''''''
For i = 0 To count
dt.Columns.Add(oDr.GetName(i), oDr.GetFieldType(i))
Next

''''''''''''''Add rows''''''''''''''
Do While oDr.Read()
Dim r As DataRow = dt.NewRow
For i = 0 To count
r(i) = oDr.Item(i)
Next

dt.Rows.Add(r)
Loop

Me.DataGridView1.DataSource = dt

Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub


Can Anyone suggest along with the codes ?

Viewing all articles
Browse latest Browse all 27193

Trending Articles