WHAT'S NEW?
Loading...

How to use Binding Navigator in VB.NET


If you’re using Windows Form DataBinding to bind data to build an application quickly, this approach can do more with a less amount of code compare with traditional approach (unbound). In this tutorial, I’ll show you how to use Binding Navigator to enable to Add, Save, Delete, Update, Retrieve, and Search record in a simple way in default generated code (see related articles for unbound or traditional approach with ADO.NET as work in the same way). This is an example screenshot of BindingNavigator after the dataset
table’s drag into the Form.
Binding navigator
As you have noticed, it generates a controls where you can navigate data and displays number of rows in a table, enable to add, delete, and save quickly. In addition, BindingNavigator allows you to customize depends how it looks like. As my own opinion, I don’t like to use this control as my default view to add, delete, save, etc. to control my data, instead I use a buttons or icons in every commands. However, if you double click an ADD icon there’s no code available how the code does is. Fortunately, to add new entry the code is simple as this.

Me.TblinfoBindingSource.AddNew(), you can add this line of code in a Form Load while adding new entry so that you if the program start running you don’t need to click add button every time to add new entry. Then, put again this code in Save Button after message box display so that it will clear all textboxes and enable you to add again new entry.

To DELETE icon, the code is:

Me.TblinfoBindingSource.RemoveAt(Me.TblinfoBindingSource.Position)
Me.TblinfoTableAdapter.Update(DbInfoDataSet.tblinfo)
MessageBox.Show("Deleted")

The code for Save and Update works on the same way depends on how to manage the source code.

Me.Validate()
Me.TblinfoBindingSource.EndEdit()
Me.TblinfoTableAdapter.Update(Me.DbInfoDataSet.tblinfo)
MessageBox.Show("New record(s) has been added to the database")

The only difference is the message box show dialog if what message will appear either new records added or records has been successfully updated.

To navigate records without using default binding navigator, the code is simple as this. Just simply add a button |<  <  >  >|.

Move first record
Me.TblinfoBindingSource.MoveFirst()

Move previous record
Me.TblinfoBindingSource.MovePrevious()

Move next record
Me.TblinfoBindingSource.MoveNext()

Move last record
Me.TblinfoBindingSource.MoveLast()

To fill a record, Me.TblinfoTableAdapter.Fill(Me.DbInfoDataSet.tblinfo)can be found in the Form Load. Actually, you can omit this line of code if you wish.

The search record the code is simple as this.

Me.TblinfoBindingSource.Filter = "Lastname LIKE '%" & txtsearch.Text & "%' or Firstname LIKE '%" & txtsearch.Text & "%'"

Here are the sample screenshots:
Add New Entry
Add New Entry
Update records
Update Records
Delete Records
Delete Records
Show Records
Show Records
Quick Search Records
Quick Search Records
Source can be download hereIf you want to save more files online and share it to your friends, sign up for free.

2 comments: Leave Your Comments