كود :
Public Class Form1
Dim str As String = "provider=microsoft.ace.oledb.12.0;data source=|DataDirectory|\db.accdb"
Dim con As New OleDb.OleDbConnection(str)
Dim dt As DataTable
Dim da As OleDb.OleDbDataAdapter
Dim cmd As OleDb.OleDbCommandBuilder
Private Function gid() As Integer
Try
Dim cm As New OleDb.OleDbCommand("select Last(tid) from tb", con)
If con.State = ConnectionState.Closed Then con.Open()
Return cm.ExecuteScalar
Catch ex As Exception
Return Nothing
End Try
End Function
Private Sub gd()
ListBox1.DataSource = Nothing
dt = New DataTable
da = New OleDb.OleDbDataAdapter("select * from tb", con)
cmd = New OleDb.OleDbCommandBuilder(da)
da.Fill(dt)
ListBox1.DataSource = dt
ListBox1.DisplayMember = "tid"
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
gd()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim row As DataRow = dt.NewRow
row(0) = gid() + 1
dt.Rows.Add(row)
da.Update(dt)
gd()
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If ListBox1.SelectedIndex < 0 Then MsgBox("اختر الرقم المراد حذفه من القائمة", MsgBoxStyle.Critical, "") : Return
dt.Rows(ListBox1.SelectedIndex).Delete()
da.Update(dt)
gd()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
ListBox1.DataSource = Nothing
For r As Integer = 0 To dt.Rows.Count - 1
Dim row As DataRow = dt.Rows(r)
row.BeginEdit()
row(0) = r
row.EndEdit()
da.Update(dt)
Next
gd()
End Sub
End Class
كود :
Private Function gid() As Integer
Try
Dim cm As New OleDb.OleDbCommand("select Last(tid) from tb", con)
If con.State = ConnectionState.Closed Then con.Open()
Return cm.ExecuteScalar
Catch ex As Exception
Return Nothing
End Try
End Function