Working With a BindingNavigator Control in Windows Form


Introduction

The BindingNavigator control provides a UI for navigating records in a form. Generally this control is used with the BindingSource control to move from a record to another record. By default this control has a series of buttons for moving to next and previous records as well as first and last records, to add a new record and to delete a record. Here we will use this control in our Windows application.

At first we should have some records. So we create a Database and insert some records into the database table.

Create Database

CREATE
DATABASE EMP

USE
EMP

CREATE TABLE EMP_DETAIL
(

 E_ID INT PRIMARY KEY,
 E_NAME VARCHAR(30),
 E_AGE INT,
 E_CITY VARCHAR(30),
 E_DEPARTMENT VARCHAR(20)
 )
 
 INSERT INTO EMP_DETAIL VALUES(11,'ALOK KUMAR',24,'DELHI','IT')
 INSERT INTO EMP_DETAIL VALUES(12,'RAJESH TRIPATHI',22,'ALLAHABAD','SALES')
 INSERT INTO EMP_DETAIL VALUES(13,'SATISH KUMAR',23,'JHANSI','PRODUCT')
 INSERT INTO EMP_DETAIL VALUES(14,'MANOJ SINGH',22,'NOIDA','MARKETING')
 INSERT INTO EMP_DETAIL VALUES(15,'AMIT MAHESHWARI',25,'ALLIGARH','IT')
 INSERT INTO EMP_DETAIL VALUES(16,'DEEPAK DWIJ',24,'NOIDA','IT')

I am showing a screen shot of all records of an EMP_DETAIL table so that it can become easy to understand.

SELECT
* FROM EMP_DETAIL




Now create a Windows Forms application. Follow the given steps.

Step 1 : Go to Toolbox and take a BindingSource and a BindingNavigator control.



Step 2 : It will add to your form like in the following figure.



Step 3 : Now we set DataSource and DataMember properties to a BindingSource object. For doing it you can get help here. Now select BindingNavigator and go to its property window. Set the "BindingSource" property to bindingSource1. Look at the following figure.



Step 4 : Take five Labels and five TextBox controls. Set the Text property of the Labels to "ID", "Name", "Age", "City", and "Department" and the name property of the TextBoxes to "txtid", "txtname", "txtage", "txtcity" and "txtdepartment". (In this example I have set these properties as given.)



Step 5 : Now select a TextBox and go to its Text property under DataBindings.



Step 6 : Click at Text property -> Click at bindingSource1 and select appropriate column. (Look at the following figure; I am selecting "E_ID" to show the value of this column in the TextBox.)



Step 7 : Do the same for the remaining TextBoxes.

Run the application.

Output



You can move to first, previous, next and last records as well and you can add new records and delete records.



Now we perform the same work without using a wizard. Take a Windows Forms application -> take a BindingSource, a BindingNavigator, five Labels and five TextBoxes and set the Text and Name property like as Step 4. Your form will look like in the following figure.



Write the following code.

using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
 
 

namespace
WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void Form2_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp_detail","database=emp;server=.;user=sa;password=wintellect");
            ds = new DataSet();
            da.Fill(ds);
            bindingSource1.DataSource = ds.Tables[0];
            bindingNavigator1.BindingSource = this.bindingSource1;
            txtid.DataBindings.Add(new Binding("Text", bindingSource1, "E_ID", true));
            txtname.DataBindings.Add(new Binding("Text", bindingSource1, "E_NAME", true));
            txtage.DataBindings.Add(new Binding("Text", bindingSource1, "E_AGE", true));
            txtcity.DataBindings.Add(new Binding("Text", bindingSource1, "E_CITY", true));
            txtdepartment.DataBindings.Add(new Binding("Text", bindingSource1, "E_DEPARTMENT", true));
        }
    }
}
 
Run the application.



You can perform different operation like above.
 

Related resources

How to Work with Items Controls  in Windows Phone 7

SharePoint Designer Work flow: Part I

Three State Work flow in Sharepoint

Working With Active MDI Child Form Controls: Part 2

Up Next
    Ebook Download
    View all
    Learn
    View all