Search This Blog

Sunday, July 17, 2011

How-To: Using BindingNavigator & BindingSource


If you like this article, please click on +1 button and share with your friends.

Click here for the Video Tutorial of this article.
visit www.iGnani.com


How to - Steps to complete the exercise:

You can see this How-To article in action in this video:



  • First we Create a Windows Form.
  • Then we add BindingNavigator & Other Controls onto the form.
  • We follow it with Creating a Data Source using Data Source Configuration Wizard.
  • Finally, we Bind the Controls on the form to the BindingSource.

You can find a more details on BindingSource and BindingNavigator on our Blog. Please feel free to look at them for a better understanding of this article.

We will be using Visual Studio .net 2010 for our demo.

Creating the form with controls on them

Open Visual Studio .net 2010, create a Windows Form project.

Let us go ahead and add a “Windows Form” to our project. To add a form, go to the Solution Explorer, right click on the Project. From the shortcut menu that opens up, select Add and then select Windows Form. This shows up a Add New Item window. Let us give our form a Name. I will call it as “BindingNavigatorDemo.cs”. After that click on the Add button. This will add a windows form onto our project. The windows form is now ready and you can see it in the design mode on your screen.

Now let us add some controls onto the form. In the demo, I will be working with three fields from the table “Employee” of “AdventureWorks2008” SQL Server database.

So, let us go and add three Label controls and three TextBox controls to the form. To do this, simple drag and drop the controls from the toolbox onto your form.

Let us Change the text of the labels. To do this go to the Properties window. Select the first label control, go to the property Text and enter the word “First Name”. Repeat the steps for second and third labels. Give them the words “Last Name” and “Job Title”.

Next, let us add the BindingNavigator Control from the toolbox.  Drag and drop it onto the form. It docks to the top of the form. Top position is the default value, while you can change it from the properties window.

Now that, we are done with designing the form. let us hook it up with the database.

Creating BindingSource and other objects

In order to bind the controls onto the fields in a database, we have to set the Text property under DataBindings group.

To do this, click on the textbox on the form, then in the properties window, expand the DataBindings property by clicking the arrow sign next to it. Select the Text property and click the drop-down arrow for the Text property.

At this point, you can see the DataSource window is empty.

Adding a DataSource using “Data Source Configuration Wizard”.

Let us then Add a Data Source to the project.  You can see the “Add Project Data Source” at the bottom of the window. Click the "Add Project Data Source" link which will invoke the “Data Source Configuration Wizard”.

The first screen that you see is the “Choose a Data Source Type” Screen, which allows you to choose the data source for your data. You are provided with various options.

  • You can click on the “Database” icon for connecting to various databases such as SQL Server, Oracle and so on.
  • The “Web Service” Icon allows you to connect to a web service.
  • The “Object” icon for connecting to business logic components.
  • SharePoint” icon to connect to SharePoint server.

For our demo, let us go with Database icon. Click on the Database icon and then click the Next button.

You will be taken to the "Choose a Database Model" page. Here select the Dataset option and click on Next button.

Next is the "Choose Your Data Connection" page. Here you can either select the existing connections that you have created earlier, or you can create a new one.

Let me go ahead and create a new connection for this demo. Click the "New Connection" button.

  • In the "Add Connection" dialog box that appears, make sure that the "Data Source" field is Microsoft SQL Server (SqlClient). If its something different, you can use the Change button next to change the Data Source type.
  • Enter the server name. For the demo, I have my SOL Server installed on the demo machine. So i will enter the words "localhost" for the server. If you have your database server on a different machine, then you can either give the Server Name or the Server IP.
  • If your server is configured to accept “Windows Authentication”, just select it and continue. Else, select the "Use SQL Server Authentication" and provide a valid User Name and Password. I will go with the Option "Use Windows Authentication".
  • Next we need to select the Database. For this click on the dropdown box and from the list that appears, select your database. For our demo we will be using AdventureWorks2008 database. Select It.
  • Test your connection settings  by clicking the Test connection button to ensure that the details you provided is Correct. Click the OK buttons that appear.

Now your will be taken back to "Choose Your Data Connection".  The connection that you just created will be selected.

If you click on the plus icon next to the Connection String, you can see the connection string that was created. Click Next.  This is where you will be selecting the Database objects that you will be binding to your form.

Here you have the option to select data directly from the Tables in your database or from the Views that are available in your database or from the Stored Procedures or from the Functions.

I will be using a view that is there in the database. So expand the View Node and then select the check box for vEmployee(HumanResources).  Next provide a name for the DataSet that will be created.  By default it will be the Database name suffixed with the word DataSet.

Since we only need three fields, let us select only those fields that we need for this demo. for this click on the checkbox next to the fields that we need. We will be using the fields FirstName, LastName and JobTitle for this demo. Once you are done with the selection, click the Finish button and you are done.

Now the wizard will generate a DataSet, a BindingSource and a TableAdapter objects for use.

Binding the Controls

Now, under the Text property for the text box on the form, you can see a new node named "Other Data Sources". You can also see that it is bound to the dataset that we just created. Expand the nodes until you find the Dataset. Now if you expand the dataset, you can see the vEmployee object with three fields that we selected. Let us go and bind the text boxes to these fields from the dataset.

Select the text box on the form and click on the Text property and expand till you find the fields and select the FirstName for the first text box. Follow the same steps for the other two textboxes. Now that we are done with binding the textboxes, let us bind the BindingNavigator control.

Select the BindingNavigator control. Since it is made up of multiple controls, make sure that you have selected it correctly by confirming it in the properties window. Next, under the properties window, under the category Data, Select the BindingSource property, click the dropdown box and select vEmployeeBindingSource. That completes the Data binding of all the controls on the form.

We are all done. Save it and execute the form.

You can see the form with the data from the view.  Test the form by clicking the Next, previous, First Record & Last record buttons to see the BindingNavigator in action.

And that brings us to the end of this How-To Article on using the BindingSource & BindingNavigator objects with a Windows Form.


visit www.iGnani.com

MicroMind Information Systems
#4013, K.R. Road, Banashankari II Stage,
BANGALORE - 560070
KARNATAKA, INDIA
Phone: +91 80 26762747


No comments:

Post a Comment