Use of BindingNavigator controls in WinForm

Source: Internet
Author: User

Use of BindingNavigator controls in WinForm
In Microsoft WinForm, the BindingNavigator control is primarily used to bind data. You can bind a data collection to the control for data

Linkage of the display effect. As shown:

So, let's take a look at the effect shown in the BindingNavigator control.
Analysis:
This case takes the BindingNavigator control as the main line, we define an entity class that displays the entity class as a collection in the DataGridView,

At the same time, bind BindingNavigator with DataGridView, bind the BindingNavigator to the text box and display the data

BindingNavigator is bound to the button control to demonstrate the effect of a data linkage that has been achieved to learn the BindingNavigator control.

Steps:
First, create a WinForm application in Visual Studio, add BindingNavigator controls in Form1, DataGridView control

Pieces, two text boxes, four buttons (respectively: First,previuos,next,last);

Second, add a user class User.cs to the project, which has two attributes name and age;
Example code:
Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Bindingdemo
{
public class User
{
Public User ()
{

}
Public User (String Name,int age)
{
This. name = name;
This. Age = age;
}
private string name;

public string Name
{
get {return name;}
set {name = value;}
}
private int age;

public int Age
{
get {return age;}
set {age = value;}
}

}
}

Again, in the Form1 code, create the user object and save the object to the List<> generic collection and display it in DataGridView;
Key code:
Create a generic collection
list<user> users = new list<user> ();

<summary>
Form Load Events
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Mainform_load (object sender, EventArgs e)
{
Create user
User zh = new User ("Zhang San", 12);
User Li = New user ("John Doe", 18);
User WW = new User ("Harry", 20);
User LL = new User ("Liu Liu", 30);
User X = New User ("X-man", 30);
User Spaider = New User ("Spaiderman", 25);
User Ironman = new User ("Ironman", 30);
To add a user to an object in a list<> generic collection
Users. ADD (ZH);
Users. Add (LI);
Users. ADD (WW);
Users. ADD (LL);
Users. ADD (X);
Users. ADD (Spaider);
Users. ADD (Ironman);
}
Again, create the BindingSource object, and in the form Load event, the BindingNavigator control, with DataGridView, text box, press

Button control for data binding, data linkage display effect;
Example code: Form1.cs
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;

Namespace Bindingdemo
{
public partial class Mainform:form
{
Create a generic collection
list<user> users = new list<user> ();
//
BindingSource bs = new BindingSource ();

Public MainForm ()
{
InitializeComponent ();
}


<summary>
Form Load Events
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Mainform_load (object sender, EventArgs e)
{
Create user
User zh = new User ("Zhang San", 12);
User Li = New user ("John Doe", 18);
User WW = new User ("Harry", 20);
User LL = new User ("Liu Liu", 30);
User X = New User ("X-man", 30);
User Spaider = New User ("Spaiderman", 25);
User Ironman = new User ("Ironman", 30);
To add a user to an object in a list<> generic collection
Users. ADD (ZH);
Users. Add (LI);
Users. ADD (WW);
Users. ADD (LL);
Users. ADD (X);
Users. ADD (Spaider);
            users. ADD (Ironman);

           // Assigns the value in the generic collection object to the DataSource of the BindingSource object
            BS . DataSource = users;
           //Show data to DataGridView
             this.dgvMain.DataSource = BS;
           //bound navigation
             this.bindingNavigator1.BindingSource = BS;
           //data binding with a text box
             this.txtName.DataBindings.Add ("Text", BS, "Name");
            this.txtAge.DataBindings.Add ("Text", BS, " Age ");
       }

Move to the first line private void Btnfirst_click (Objectsender, EventArgs e)
{
This.bs.MoveFirst ();
}

Move to Previous line
private void Btnprevious_click (object sender, EventArgs e)
{
This.bs.MovePrevious ();
}

Move to the next line
private void Btnnext_click (object sender, EventArgs e)
{
This.bs.MoveNext ();
}

Move to the last line
private void Btnlast_click (object sender, EventArgs e)
{
This.bs.MoveLast ();
}
}
}

Use of BindingNavigator controls in WinForm

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.