How bindingnavigator binds to the datagridview

Source: Internet
Author: User
   1: BindingSource bs = new BindingSource();

   2: bs.DataSource = dateTabel1;

   3: bindingNavigator1.BindingSource = bs;

   4: dataGridView1.DataSource = bs ;

The bindingnavigator control is generally easier to use with the bindingsource control, because each button on the bindingnavigator control has a corresponding bindingsource component member, which can have the same functions programmatically. For example, the movefirstitem button corresponds to the movefirst method of the bindingsource component, and the deleteitem button corresponds to the removecurrent method. AlthoughBindingnavigatorCan be bound to any data source, but it is designed to use its bindingnavigator. bindingsource attributeBindingsourceComponent Integration.

Therefore, define a bindingsource and set the data sources of bindingnavigator and datagridview as bindingsource to ensure data synchronization between bindingnavigator and datagridview.

 

 

Bytes ----------------------------------------------------------------------------------------------------------------------

Bindingsource control Introduction

The bindingsource control is one of the new controls provided by. NET Framework 2.0. The bindingsource control establishes a connection with the data source, and then establishes a binding relationship between the control in the form and the bindingsource control to bind data and simplify the data binding process.

The bindingsource control is a channel for connecting to the background database and a data source. Because the bindingsource control supports sending commands to the background database to retrieve data, you can directly access, sort, filter, and update data using the bindingsource control. The bindingsource control can automatically manage many binding problems.

The bindingsource control does not have a runtime interface and cannot be seen on the user interface.

The bindingsource control accesses the current record through the current attribute and the entire data table through the list attribute.

The following table listsMain attributes:

Attribute description

Allowedit indicates whether records in the bindingsource control can be edited.

Allownew indicates whether the addnew method can be used to add records to the bindingsource control.

Allowremove indicates whether records can be deleted from the bindingsource control.

Count to obtain the number of records in the bindingsource control.

Currencymanager gets the current record manager associated with the bindingsource control.

Current gets the current record in the bindingsource Control

Datamember obtains or sets a specific data list or database table in the data source to which the connector is currently bound.

Datasource gets or sets the data source to which the connector is bound.

Filter gets or sets the expression used for filtering.

Item gets or sets the record of the specified index.

Sort gets or sets the column name used for sorting to specify sorting.

The current attribute and the removecurrent, endedit, canceledit, add, and addnew methods can be used to edit the current record.


The following table listsMain Method

Method description

Add add existing items to the internal list

Canceledit removes all elements from the list

Endedit applies the pending changes to the basic data source.

Find finds the specified item in the data source.

Move movefirst to the first item in the list.

Move movelast to the last item in the list.

Move movenext to the next item in the list.

Move moveprevious to the previous item in the list.

Removecurrent removes the current item from the list.


If you drag a table from the [Data Source] To the datagridview and data generated on the form, use the bindingnavigator automatically generated by vs05 to add, delete, and modify the data. You do not need to write a line of code. In addition, we strongly recommend using bindingsource as the middle layer between the control and data from vs05. That is to say, the control is bound to bindingsource, and bindingsource is then bound to date item or data item list ). This has many advantages. Bindingsource. endedit (); submits the update to the list of objects or objects (such as dataset) in the memory ). The bindingsource. Update method submits the update to the database.
Take vs2005 as an example to operate the access2003 database (without a wizard)

Create an Access 2003 file db1.mdb and create a table (person): The table structure is as follows, enter some content
UID auto-numbered primary key

Name text
Age number
Sex text
Create a winform in vs2005 and drag a dview and button to the form. Run the command and copy db1.mdb to the DEBUG directory. Add three members to the class:

Private datatable DT;

Private bindingsource BS;

Private oledbdataadapter da;

Double-click form1. In form1_load:

Private void form1_load (Object sender, eventargs E)

{

//// Todo: This line of code loads data into the "db1dataset. Stu" table. You can move or remove it as needed.

// This. stutableadapter. Fill (this. db1dataset. Stu );

Oledbconnection conn = new oledbconnection ();

Conn. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source = db1.mdb ";

Oledbcommand cmd = new oledbcommand ();

Cmd. Connection = conn;

Cmd. commandtext = "select * From person ";

Dt = new datatable ();

DA = new oledbdataadapter ();

Da. selectcommand = cmd;

Oledbcommandbuilder cb = new oledbcommandbuilder (DA );

Da. Fill (DT );

BS = new bindingsource ();

BS. datasource = DT;

Datagridview1.datasource = BS;

}

Double-click button1.
Private void button#click (Object sender, eventargs E)
{
Da. Update (DT );
MessageBox. Show ("OK ");
}
In my example

Private bindingsource BS; // remove

BS = new bindingsource ();

BS. datasource = DT;

Datagridview1.datasource = BS; // you can directly use datasource = DT without using bindingsource;

Change these three lines

Datagrid1.datasource = DT;

---------

Keep in mind: In applications using dataadapter, you do not need to manually open or close the connection.

That is, you should not see conn. open (), conn. Close () or other things in your code.

Because dataadapter automatically opens and closes the connection for you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.