How to Bind a BindingNavigator With a TextBox in Windows Forms


Introduction

In this article, I am binding a BindingNavigator with a TextBox control. The BindingNavigator control provides a series of buttons for moving to the next, previous, first and last records as well as adding and deleting records. After binding with a TextBox, we can perform these operations using TextBox. Let's bind a BindingNavigator with a TextBox control. Here, we will do it in different ways.

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




First, we do it using the wizard. 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 click the EMP_DETAIL to explore all columns of the table.



Step 13 : Drag a column and drop it onto the form. Repeat this process and drop all columns on the form.



Run the application.



You can show the first, last, next and previous record in the textbox by clicking buttons of the
BindingNavigator control. You can add new records, delete records and update records as well.

Now we do the same without using the wizard.

Create a another form and add a BindingNavigator, five Labels and five TextBoxes. Set the Text property of Labels as "ID", "Name", "Age", "City", and "Department" and the Name property of the TextBoxes as "txtid", "txtname", "txtage", "txtcity" and "txtdepartment".



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
bindingnavigatorwithtextbox
{
    public partial class Form1 :
Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter dadapter;
        DataSet dset;
        BindingSource bsource;
        string connstring = "database=emp;server=.;user=sa;password=wintellect";
        private void Form1_Load(object sender, EventArgs e)
        {
            dadapter = new SqlDataAdapter("select * from emp_detail", connstring);
            dset = new DataSet();
            dadapter.Fill(dset);
            bsource = new BindingSource();
            bsource.DataSource = dset.Tables[0];
            bindingNavigator1.BindingSource = this.bsource;
            txtid.DataBindings.Add(new Binding("Text", bsource, "E_ID", true));
            txtname.DataBindings.Add(new Binding("Text", bsource, "E_NAME", true));
            txtage.DataBindings.Add(new Binding("Text", bsource, "E_AGE", true));
            txtcity.DataBindings.Add(new Binding("Text", bsource, "E_CITY", true));
            txtdepartment.DataBindings.Add(new Binding("Text", bsource, "E_DEPARTMENT", true));
        }
    }
}

Run the application



You can perform all the same operations as above.


Here are some related resources.

Working With a BindingNavigator Control in Windows Form

How to Bind a BindingNavigator with a DataGridView in Windows Forms

Bind ScrollBarToTextBox and TextBoxToScrollBar in Silverlight

Enter Hot key on textbox in c#


Similar Articles