在微软WinForm中,BindingNavigator控件主要用来绑定数据。可以将一个数据集合与该控件绑定,以进行数据联动的显示效果。如图下图所示:
那么,下面我们就来用BindingNavigator控件做一下上图所示的效果。
分析:该案例以BindingNavigator控件为主线,我们定义一个实体类,将实体类以集合的形式显示在DataGridView中,同时,将BindingNavigator与DataGridView绑定,将BindingNavigator与文本框显示数据绑定,将BindingNavigator与Button控件绑定,来演示一个数据联动的效果,已达到学习BindingNavigator控件的目的。
步骤:
首先,在Visual Studio中创建一个WinForm应用程序,在Form1中添加BindingNavigator控件,DataGridView控件,两个文本框,四个按钮(分别表示:First,Previuos,Next,Last);
其次,在项目中添加一个用户类User.cs,该类中有两个属性Name和Age;
示例代码:
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; }
}
}
}
再次,在Form1的代码之中,创建User对象,并将对象保存到List<>泛型集合之中,并显示到DataGridView中;
关键代码:
//创建一个泛型集合
List<User> users = new List<User>();
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
//创建用户
User zh = new User("张三", 12);
User li = new User("李四",18);
User ww = new User("王五",20);
User ll = new User("刘六",30);
User X = new User("x-man",30);
User spaider = new User("spaiderman",25);
User ironman = new User("ironman",30);
//将用户添加到List<>泛型集合的对象中
users.Add(zh);
users.Add(li);
users.Add(ww);
users.Add(ll);
users.Add(X);
users.Add(spaider);
users.Add(ironman);
}
再次,创建BindingSource对象,并在窗体加载事件中将BindingNavigator控件,与DataGridView,文本框,按
钮控件进行数据绑定,进行数据联动显示效果;
示例代码: 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
{
//创建一个泛型集合
List<User> users = new List<User>();
//
BindingSource bs = new BindingSource();
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
//创建用户
User zh = new User("张三", 12);
User li = new User("李四",18);
User ww = new User("王五",20);
User ll = new User("刘六",30);
User X = new User("x-man",30);
User spaider = new User("spaiderman",25);
User ironman = new User("ironman",30);
//将用户添加到List<>泛型集合的对象中
users.Add(zh);
users.Add(li);
users.Add(ww);
users.Add(ll);
users.Add(X);
users.Add(spaider);
users.Add(ironman);
//将泛型集合对象中的值赋给Bindingsource对象的DataSource
bs.DataSource = users;
//将数据显示到DataGridView中去
this.dgvMain.DataSource = bs;
//绑定导航
this.bindingNavigator1.BindingSource = bs;
//与文本框进行数据绑定
this.txtName.DataBindings.Add("Text",bs,"Name");
this.txtAge.DataBindings.Add("Text",bs,"Age");
}
//移动到第一行
private void btnFirst_Click(object sender, EventArgs e)
{
this.bs.MoveFirst();
}
//移动到前一行
private void btnPrevious_Click(object sender, EventArgs e)
{
this.bs.MovePrevious();
}
//移动到下一行
private void btnNext_Click(object sender, EventArgs e)
{
this.bs.MoveNext();
}
//移动到最后一行
private void btnLast_Click(object sender, EventArgs e)
{
this.bs.MoveLast();
}
}
}
<l来源:http://blog.163.com/qqabc20082006@126/blog/static/229285252009718103810534/>