How to Bind a BindingNavigator with a DataGridView in Windows Forms

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 moving next, previous, first, and last records 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 screenshot of all records of an EMP_DETAIL table so that it can become easy to understand.

SELECT * FROM EMP_DETAIL

Select from emp detail-

Windows form application setup steps

Take a Windows Form Application and follow the given steps.

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

Add new data source-

Step 2. A new window will be open.

Data source configuration next-

Step 3. Click the Next button.

Choose your database model-

Step 4. Click the Next button.

Choose your Data connection-

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

Add test connection  -

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

Add connection emp-

Step 7. Click the 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.

Data source configuration wizard new connection-

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

Save the connection string to the application-

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

Data source configuration tables-

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.

Choose your database objects-

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.

Form-1-

Run the application.

Form-1 ID-

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 the same work without using the wizard. Take a BindingNavigator and DataGridView control at form. The form will look like the following figure.

Form-2

Write the following code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace databindingusingbindingnavigatorapps
{
    public partial class Form2 : Form
    {
        SqlDataAdapter dadapter;
        DataSet dset;
        BindingSource bs;
        string connstring = "database=emp;server=.;user=sa;password=wintellect";

        public Form2()
        {
            InitializeComponent();
        }

        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

Form-2 ID

Now you can perform the same work as above.

Here are some related resources


Similar Articles