我有一个sdf数据库设置。单击movenext按钮时出现问题。因此,当我继续浏览记录时,按顺序进行:23,24,25, 32 ,26,27,28,29,30,31,33,34 ...等等。 但它应该是25,26,27,28,29,30,31,32 ..等等吧?
这些数字是自动生成的记录ID,每个新记录增加
当我在datagridview中查看记录时,顺序是好的。那么我有什么想法可以解决这个问题?
更新 这么晚才回复很抱歉。我一直在尝试为记录导航创建一个替代路线,以便按ID号顺序排列。我创建了一个变量 Cur_navID ,其中保存了当前记录ID。每次按下下一个或上一个按钮时,将显示下一个按顺序记录。但有数千条记录,这种方法往往有点模糊。所以无论如何都是主要代码
以下是代码:
新纪录
Private Sub NewRecord()
If Not isEditing = True Then
Enable_edit()
id += 1
Me.BindingNavigatorAddNewItem.PerformClick()
Cust_nameTextBox.Clear()
ContactTextBox.Clear()
RemTextBox.Clear()
GradeTextBox.Clear()
SchoolTextBox.Clear()
V_numberTextBox.Clear()
Student_nameTextBox.Clear()
M7_idLabel1.Text = String.Format("{0:0000}", id)
date_pick.Value = DateTime.Now
StatusComboBox.SelectedIndex = 0
StatusComboBox.Text = "To Be Done"
setStatus(StatusComboBox.Text)
Cust_nameTextBox.Focus()
ElseIf isEditing = True Then
Dim di As DialogResult = MsgBox("Do you want to save the current record?", MsgBoxStyle.YesNoCancel)
If di = Windows.Forms.DialogResult.Yes Or di = Windows.Forms.DialogResult.OK Then
SaveRecord()
NewRecord()
ElseIf di = Windows.Forms.DialogResult.No Then
Canceledit()
ElseIf di = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
End If
End Sub
保存记录
Private Sub SaveRecord()
Try
'// for customers with multiple lists. easier entry
lastmember.cust_name = Cust_nameTextBox.Text
lastmember.contact = ContactTextBox.Text
lastmember.grade = GradeTextBox.Text
lastmember.remks = RemTextBox.Text
Cust_nameTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource
Cust_nameTextBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
Dim MySource As New AutoCompleteStringCollection()
MySource.Add(lastmember.cust_name)
Cust_nameTextBox.AutoCompleteCustomSource = MySource
Me.ListsBindingNavigatorSaveItem.PerformClick()
Disable_edit()
MsgBox("Record Saved!", MsgBoxStyle.Information)
'AppWait(700)
'ListsBindingSource.MoveLast()
Cust_nameTextBox.Focus()
Catch ex As Exception
' MsgBox("Please Fill in Customer Name, Contact and Grade Boxes", MsgBoxStyle.Information)
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
End Sub
绑定导航器保存项目代码
Private Sub ListsBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles ListsBindingNavigatorSaveItem.Click
Me.Validate()
Me.ListsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.BooksListDataSet)
My.Settings.ID = id
My.Settings.Save()
End Sub
并在表单加载事件中
' ### Load data into the 'BooksListDataSet.Lists' table.
Me.ListsTableAdapter.Fill(Me.BooksListDataSet.Lists)
Me.BindingNavigatorMoveLastItem.PerformClick()
' ### Update counter if database changed
Dim chek As Boolean = MAXID(BooksListDataSet.Tables("Lists")) = String.Format("{0:0000}", My.Settings.ID.ToString)
If chek = False Then
MsgBox("Database has been updated. Application will restart to compensate the changes.", MsgBoxStyle.Information)
' My.Settings.ID = Me.BooksListDataSet.Tables("Lists").Rows(BooksListDataSet.Tables("Lists").Rows.Count - 1).Item(0).ToString
My.Settings.ID = MAXID(BooksListDataSet.Tables("Lists"))
My.Settings.Save()
Application.Restart()
Else
End If
' ### Take backup
If My.Settings.backup_date = Nothing Then
Dim di As DialogResult = MsgBox("Do you want to create a backup database?", MsgBoxStyle.YesNoCancel)
If di = Windows.Forms.DialogResult.Yes Or di = Windows.Forms.DialogResult.OK Then
'//backup database
If Not My.Computer.FileSystem.FileExists(Path.Combine(Application.StartupPath.ToString, My.Settings.ID.ToString & "BooksList.sdf.BAK")) Then
My.Computer.FileSystem.CopyFile(Path.Combine(Application.StartupPath.ToString, "BooksList.sdf"), Path.Combine(Application.StartupPath.ToString, My.Settings.ID.ToString & "BooksList.sdf.BAK"))
My.Settings.backup_date = Date.Today
My.Settings.Save()
End If
Else
'do nothing
End If
ElseIf Not My.Settings.backup_date = Date.Today Or My.Computer.FileSystem.FileExists(Path.Combine(Application.StartupPath.ToString, My.Settings.ID.ToString & "BooksList.sdf.BAK")) = False Then
Dim di2 As DialogResult = MsgBox("Do you want to create todays backup database now?", MsgBoxStyle.YesNoCancel)
If di2 = Windows.Forms.DialogResult.Yes Or di2 = Windows.Forms.DialogResult.OK Then
'//backup database
If Not My.Computer.FileSystem.FileExists(Path.Combine(Application.StartupPath.ToString, My.Settings.ID.ToString & "BooksList.sdf.BAK")) Then
My.Computer.FileSystem.CopyFile(Path.Combine(Application.StartupPath.ToString, "BooksList.sdf"), Path.Combine(Application.StartupPath.ToString, My.Settings.ID.ToString & "BooksList.sdf.BAK"))
My.Settings.backup_date = Date.Today
My.Settings.Save()
End If
Else
'do nothing
End If
End If
' ### Load Autopcomplete list
Dim autocompleteList As New System.Windows.Forms.AutoCompleteStringCollection
Using reader As New System.IO.StreamReader("Schools.ACL")
While Not reader.EndOfStream
autocompleteList.Add(reader.ReadLine())
End While
End Using
SchoolTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource
SchoolTextBox.AutoCompleteMode = AutoCompleteMode.Suggest
SchoolTextBox.AutoCompleteCustomSource = autocompleteList
' ### Focus next textbox on enter function part
textBoxes.Add(Cust_nameTextBox)
textBoxes.Add(ContactTextBox)
textBoxes.Add(RemTextBox)
textBoxes.Add(GradeTextBox)
textBoxes.Add(SchoolTextBox)
textBoxes.Add(V_numberTextBox)
textBoxes.Add(Student_nameTextBox)
Disable_edit()
Cur_navID = MAXID(Me.BooksListDataSet.Tables("Lists"))
Me.ListsBindingSource.Position = Cur_navID