Use Navigation Buttons in DataTable Using F#

Introduction

In this arcticle you will learn about the ToolStrip button and properties of the BindingNavigator class in a Windows Forms Application. The main purpose of this article is to explain how to use the navigation buttons for a DataTable.

Properties of BindingNavigator Class

The BindingNavigator class navigates and manipulates the user data in the form they are in; The following are some properties of the bindingnavigator class:

  • Top: Top click gets the first record of the table field.
  • Next: Next click gets the next record of the table field.
  • Prev: Previous click gets the previous record of the table field.
  • Bottom: Bottom click gets the last record of the table field.
  • Delete: Delete click deletes the selected item of the table field.

ToolStrip Button

The ToolStrip Button is a toggle button that has the two states of an ordinary state and a selected state. When you select the toggle button it remains highlighted until the mouse is not hovering over it. The user can change the state of the toggle button by clicking on it once.

Syntax: let movefirst=new ToolStripButton(Text="Top"), initializes a new instance of the ToolStripButton class that displays the specified text.

Use the following procedure to create the database and table in SQL Server:

Create Database Employee

use Employee

create table EmployeeSalary

(

EmpId int primary key,

EmpName varchar(max),

Salary money 

)

Use the following procedure to insert the values in the database table in SQL Server:

insert into EmployeeSalary values(101,'Pankaj',20000)

insert into EmployeeSalary values(102,'Nimit',15000)

insert into EmployeeSalary values(103,'Pravesh',16000)

insert into EmployeeSalary values(104,'Amit',12000)

insert into EmployeeSalary values(105,'Ravi',25000)

insert into EmployeeSalary values(106,'Ainul',30000)

Use the following procedure to execute the query in the database table in SQL Server:

select * from EmployeeSalary 

 

DatabaseSchema

 Now  I will show you how to use navigational buttons in a Windows Forms application. Let's use the following procedure.

Step 1:

Open Visual Studio, then select "Create New Project" --> "F# Console Application".

CreateApplication

Step 2:

Now go the Solution Explorer, on the right side of the application. Right-click on "References" and select "Add references".

SelectReferences

 

AddReferences

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms", "System.Drawing" and "System.Data" while holding down the Ctrl key and click on "Ok".

ImportNamespaces

Step 4:

Use the following code to add navigation buttons to the Windows Forms Application.

open System 

open System.Windows.Forms 

open System.Data 

open System.Data.SqlClient

open System.Drawing 

  

let constring = @"Data Source=MCNDESKTOP34;Initial Catalog=Employee;User Id=; Password="

let con = new SqlConnection(constring) 

let dataadpter=new SqlDataAdapter("Select * from EmployeeSalary", con)

let dt=new DataTable()

dataadpter.Fill(dt)|>ignore

let ds = new DataSet()  

dataadpter.Fill(ds,"empInfo")|>ignore 

let datagrid=new DataGridView(Top=20,Left=40,Width=330,Height=180)

datagrid.DataSource<-dt   

let navbuttonform = new Form(Text="Use Navigational Buttons"

//creates User controls   

let lblname=new Label(Text="Employee Name:",Top=240,Left=0,Width=120) 

let lblsalary=new Label(Text="Salary:",Top=280,Left=0,Width=40) 

let namelabel=new Label(Top=240,Left=130,BorderStyle=BorderStyle.FixedSingle,Width=80) 

let salarylabel=new Label(Top=280,Left=130,BorderStyle=BorderStyle.FixedSingle) 

let bindsource=new BindingSource() 

//creates a binding navigator 

let navigationbind=new BindingNavigator(Dock=DockStyle.None,Top=200,Left=40) 

let movefirst=new ToolStripButton(Text="Top"

let moveprev=new ToolStripButton(Text="Prev"

let movenext=new ToolStripButton(Text="Next"

let movelast=new ToolStripButton(Text="Bottom"

let exitbutton=new ToolStripButton(Text="Exit"

let Deletebutton=new ToolStripButton(Text="Delete"

navigationbind.Items.Add(movefirst)|>ignore 

navigationbind.Items.Add(moveprev)|>ignore 

navigationbind.Items.Add(movenext)|>ignore 

navigationbind.Items.Add(movelast)|>ignore 

navigationbind.Items.Add(exitbutton)|>ignore 

navigationbind.Items.Add(Deletebutton)|>ignore 

//funtions for the each navigational

navigationbind.MoveFirstItem<-movefirst 

navigationbind.MoveNextItem<-movenext 

navigationbind.MovePreviousItem<-moveprev 

navigationbind.MoveLastItem<-movelast 

navigationbind.DeleteItem<-Deletebutton

exitbutton.Click.Add(fun exit->  

                    navbuttonform.Close() 

                    con.Close())   

bindsource.DataSource<-ds  

bindsource.DataMember<-"empInfo"  

navigationbind.BindingSource<-bindsource   

con.Open()   

//Binding UserControl

navbuttonform.Controls.Add(datagrid)

navbuttonform.Controls.Add(lblname) 

navbuttonform.Controls.Add(lblsalary) 

navbuttonform.Controls.Add(namelabel) 

navbuttonform.Controls.Add(salarylabel) 

navbuttonform.Controls.Add(navigationbind)  

namelabel.DataBindings.Add(new Binding("Text",bindsource,"EmpName")) 

salarylabel.DataBindings.Add(new Binding("Text",bindsource,"Salary")) 

navbuttonform.Show() 

Application.Run(navbuttonform)

 

In the code above I have used the "bindsource" class. The bindsource class has the ability to retrieve the data from the datasource and the datasource property has the defauult property of the bindsource class.

Step 5:

Debug the application by pressing F5 to debug the Windows Forms application. After debugging the application the output will be as in the following figure:

AfterDebug

Step 6 :

Now click on "Next"; the navigation will display the next record from the datatable as shown in the following figure:

NextButton

Step 7 :

Now click on the Bottom navigation; it will display the bottom record of the datatable as shown in the following figure:

BottomClick

Step 8 :

Now click on the Top navigation; it will display the Top record of the datatable as shown in the following figure:

TopClick

Step 9 :

Now click on the Delete navigation; it will delete the selected items from the datatable.

DeleteClick

Step 10 :

Now click on the Previous navigation; it will display the previous record of the datatable as shown in the following figure:

PrevClick

Summary

This article explained the ToolStrip button, the properties of the BindingNavigator class and you saw how to use navigational buttons in a Windows Forms application.


Similar Articles