Reader Level:
ARTICLE

How to Bind a BindingNavigator with a DataGridView in Windows Forms

Posted by A K Articles | ADO.NET January 27, 2012
In this article, I am binding a BindingNavigator with a DataGridView control.
  • 0
  • 0
  • 41051

Introduction

In this article, I am binding
a BindingNavigator with a DataGridView control. The BindingNavigator control provides a UI for navigating records in a form. It has a series of buttons for move next, previous, first, last record as well as adding and deleting records.

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




Take a Windows Form Application and follow the given steps.

Step 1 : Go to Data Sources and click at "Add New Data Source".



Step 2 : A new window will be open.



Step 3 : Click the Next button.



Step 4 : Click the Next button.



Step 5 :
Click the "New connection" button to make a new database connection.



Step 6 : Write the server name, user name, password and select your Database name. Click ok button.



Step 7 : Click ok button. You will be back to "Data Source Configuration Wizard". Check the radio button for "Yes, include sensitive data in the connection string " and click the next button.



Step 8 : A new window will open asking to save the connection string.



Step 9 : Click the Next button. In the new window click the Table node to explore all the tables of your database.




Step 10 :
Click the table node to explore all columns. Then check the checkbox for each column to select them. Look at the following figure.



Step 11 : Click the finish button.

Step 12 : Go to the Data Source and drag "EMP_DETAIL" onto your form. The form will look like the following figure.


Run the application.



You can move to the first and last record, next and previous records as well as you can add, delete and update records using the BindingNavigator without writing code.

Now we do same work without using the wizard. Take a BindingNavigator and DataGridView control at form. The form will look like 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
databindingusingbindingnavigatorapps
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlDataAdapter dadapter;
        DataSet dset;
        BindingSource bs;
        string connstring = "database=emp;server=.;user=sa;password=wintellect";
        private void Form2_Load(object sender, EventArgs e)
        {
            dadapter = new SqlDataAdapter("select * from emp_detail", connstring);
            dset = new DataSet();
            dadapter.Fill(dset);
            bs = new BindingSource();
            bs.DataSource = dset.Tables[0].DefaultView;
            bindingNavigator1.BindingSource = bs;
            dataGridView1.DataSource = bs;
        }
    }
}

Run the application.

Output



Now you can perform same work as above.


Here are some related resources

Filterable DataGridView using Linq and reflections

Databinding with DataGridView in ADO.NET

Bind Objects to a DataGridView Control

COMMENT USING

Trending up