The BindingSource class is used to simplify data binding as well as various operations on records. It has different methods like AddNew( ), MoveFirst( ), MovePrevious( ), MoveNext( ), etc which provide easier way for adding new row, moving to first record, moving to previous record, moving to next record and many other operations without writing code for them. In this article I will use it in a Windows Forms application. Here, the database name is "STUDENT" and the database table is "STUDENT_DETAIL" which has some records.
At first, crete a Windows Forms Application. Now follow the given steps.
Step 1 : Go to Toolbox and select bindingSource by double-clicking it or draging it.
Step 2 : It will be visible at the bottom of the form.
Step 3 : Go to the property window of the bindingSource control. Click at DataSource.
Step 4 : Click at Add Project DataSource. A new window will be open.
Step 5 : Select Database and click next button.
Step 6 : Select Dataset and click next button.
Step 7 : Click the "New Connection" button. A new window will be opened for adding connections to your database. Write the server name, user name, password and select your Database name.
Step 8 : Click the ok button. A new window will be open. It will ask to save the connection string.
Step 9 : Click the Next button. In hte 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 property window of the bindingSource. Click at DataMember and select "STUDENT_DETAIL" ( Your database table name)
Now take four Labels and four TextBox controls. And arrange them like as in the following figure.
Now bind the TextBoxes. Select the appropriate TextBox and go to the properties and set the text property. Like, I am binding the textbox to show roll_no from the database. So, I have changed the name to "txtrollno" of the TextBox and set the text under DataBinding property to "ROLL_NO". Look at the following figure.
Do the same for other TextBoxes. Then run the application.
You saw in the output window that it is showing records from the table "STUDENT_DETAIL". Now we will perform an operation using methods of the BindingSource class. Add some buttons and change the text.
Write the following code.
public partial class Form1 : Form
private void Form1_Load(object sender, EventArgs e)
// TODO: This line of code loads data into the 'sTUDENTDataSet.STUDENT_DETAIL' table. You can move, or remove it, as needed.
private void btnfirst_Click(object sender, EventArgs e)
private void btnnext_Click(object sender, EventArgs e)
private void btnprevious_Click(object sender, EventArgs e)
private void btnlast_Click(object sender, EventArgs e)
private void btnsavechanges_Click(object sender, EventArgs e)
private void btncancelchanges_Click(object sender, EventArgs e)
private void btnremove_Click(object sender, EventArgs e)
Run the application. Initially it will show the first record.
Click the "Next" button. It will show the next record.
In the sam manner, click "Previous" to go to the previous record, click "Next" to go to the next record, "First" button to the first record and "Last" button to go to the last record. You can save the changes to the current record as well as cancel the changes.
Here are some related resources