在进行一些基础的数据库操作时,使用BindingNavigator控件可以为我们的用户界面带来更多的功能,使表单更加直观、易用。BindingNavigator是WinForms中的一个控件,在设计时直观的提供一组导航按钮,包括:新建、删除、保存、上一个、下一个、查找等。
BindingNavigator控件是从数据源中获取数据并将其绑定到控件上,当用户对控件进行操作时,可以将更改写回到数据源中。在许多图形应用程序中,为表单增加BindingNavigator控件将非常有帮助。BindingNavigator控件支持多种数据源,包括:ADO、DataSet、DataTable等。下面是一个用于管理教师姓名和年龄的基本示例。
1、在Visual Studio2019中创建一个名称为“Bdnv的Windowns应用程序” (Visual Basic版本)的项目。
2、创建一个Windows表单并进行控件布置,添加TextBox和Label控件以显示教师的姓名和年龄。
3、在Solution Explorer中,右键单击“Form1”的设计视图,单击“Properties”选项。在“Properties”窗口中,找到“Data Sources”选项卡。选中“Data Sources”选项卡,并单击“Add New Data Source”。
4、在“Data Source Configuration Wizard”中,选择“Database”并单击“Next”。在下一步中,选择“DataSet”并单击“Next”。
5、在下一步中,选择用于访问数据库的连接字符串,以及要连接的数据库表。在这个例子中,我们选择一个包含教师姓名和年龄的表作为数据源。
6、在下一步中,我们还需要指定DataSet名称和TableAdapter名称。TableAdapter可用于填充DataSet并将DataSet中的更改写入数据库。我们使用默认名称“TeachersDataSet”和“TeachersTableAdapter”。
7、在下一步中,我们可以预览表中的列和数据,单击完成后完成向工程添加数据源。
8、现在,我们可以将控件绑定到数据源,方法是单击控件上的“Data Sources”标签,并在拖动适当的列到控件上时,使用自动生成的DataSet。我们需要将TextBox绑定到数据源的“Name”列,将暂时未使用的Label绑定到数据源的“Age”列。
9、现在,我们需要打开并编辑“Form1.vb”代码文件,以添加集成的BindingNavigator控件。首先,在顶部添加以下代码:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TeachersDataSet.Teachers' table. You can move, or remove it, as needed.
Me.TeachersTableAdapter.Fill(Me.TeachersDataSet.Teachers)
End Sub
接下来,在“Form1”类定义之后添加BindingNavigator控件,代码应如下:
Partial Public Class Form1
Inherits System.Windows.Forms.Form
Private bindingNavigator1 As System.Windows.Forms.BindingNavigator
Private Sub InitializeComponent()
Me.bindingNavigator1 = New System.Windows.Forms.BindingNavigator(Me.components)
CType(Me.bindingNavigator1,System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'bindingNavigator1
'
Me.bindingNavigator1.AddNewItem = Me.bindingNavigatorAddNewItem
Me.bindingNavigator1.BindingSource = Me.teachersBindingSource
Me.bindingNavigator1.CountItem = Me.bindingNavigatorCountItem
Me.bindingNavigator1.DeleteItem = Me.bindingNavigatorDeleteItem
Me.bindingNavigator1.ImageScalingSize = New System.Drawing.Size(20, 20)
Me.bindingNavigator1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.bindingNavigatorMoveFirstItem, Me.bindingNavigatorMovePreviousItem, Me.bindingNavigatorSeparator, Me.bindingNavigatorPositionItem, Me.bindingNavigatorCountItem, Me.bindingNavigatorSeparator1, Me.bindingNavigatorMoveNextItem, Me.bindingNavigatorMoveLastItem, Me.bindingNavigatorSeparator2, Me.bindingNavigatorAddNewItem, Me.bindingNavigatorDeleteItem, Me.teachersBindingNavigatorSaveItem})
Me.bindingNavigator1.Location = New System.Drawing.Point(0, 0)
Me.bindingNavigator1.MoveFirstItem = Me.bindingNavigatorMoveFirstItem
Me.bindingNavigator1.MoveLastItem = Me.bindingNavigatorMoveLastItem
Me.bindingNavigator1.MoveNextItem = Me.bindingNavigatorMoveNextItem
Me.bindingNavigator1.MovePreviousItem = Me.bindingNavigatorMovePreviousItem
Me.bindingNavigator1.Name = "bindingNavigator1"
Me.bindingNavigator1.PositionItem = Me.bindingNavigatorPositionItem
Me.bindingNavigator1.Size = New System.Drawing.Size(507, 27)
Me.bindingNavigator1.TabIndex = 0
Me.bindingNavigator1.Text = "bindingNavigator1"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(507, 186)
Me.Controls.Add(Me.bindingNavigator1)
Me.Controls.Add(Me.ageLabel)
Me.Controls.Add(Me.nameTextBox)
Me.Controls.Add(Me.ageTextBox)
Me.Controls.Add(Me.nameLabel)
Me.Name = "Form1"
Me.Text = "Teachers Table"
CType(Me.bindingNavigator1,System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.teachersBindingSource,System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.teachersDataSet,System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(false)
Me.PerformLayout()
End Sub
End Class
10、接下来,我们需要添加一些事件处理程序来处理BindingNavigator控件上的各种按钮,使它们能够正确工作。我们可以像下面这样编写代码:
Private Sub teachersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles teachersBindingNavigatorSaveItem.Click
Me.Validate()
Me.teachersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TeachersDataSet)
End Sub
Private Sub bindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
Me.teachersBindingSource.AddNew()
End Sub
Private Sub bindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
Me.teachersBindingSource.RemoveCurrent()
End Sub
Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByToolStripButton.Click
Try
Me.TeachersTableAdapter.FillBy(Me.TeachersDataSet.Teachers, NameToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
11、最后,我们需要在“Form1_Load”事件处理程序中,添加一行代码来填充数据集并显示数据:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TeachersDataSet.Teachers' table. You can move, or remove it, as needed.
Me.TeachersTableAdapter.Fill(Me.TeachersDataSet.Teachers)
End Sub
现在我们就完成了整个处理过程。我们可以运行应用程序并使用BindingNavigator控件,在表中添加、删除、查找教师名称和年龄。
在本文示例中,我们使用了BindingNavigator控件并进行了基本的配置。使用BindingNavigator可以使表单体验更加方便和友好,可以方便的完成基础的数据操作,提升了用户的操作体验。