今天我们谈谈,TextBox控件数据绑定,很多人肯定学习过数据绑定,首先创建一个winForm项目,然后我们谈谈今天需要了解东西,做一个数据导航上一条,下一条,首条,尾条,都会想到用基本的ADO.Net来实现,这种方式不是做不出来,性能上面肯定有问题,我这此要说的一个类就用这个BindingManagerBase类实现.
接下来我们来看看代码
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Data.SqlClient;
8private SqlDataAdapter ad;
9private DataSet ds;
10private BindingManagerBase bManager;//用于数据导航
11private System.Windows.Forms.Button btnFirst;
12private System.Windows.Forms.Button btnLast;
13private System.Windows.Forms.Button btnUp;
14private System.Windows.Forms.Button btnDown;
15private System.Windows.Forms.TextBox txtUserID;
16private System.Windows.Forms.TextBox txtUserName;
17private void Form1_Load(object sender, System.EventArgs e)
18{
19 string strConn = "Server=.; uid=sa; pwd=sa; database=Test";
20 string strSql = "Select UserID,UserName From UserTest";
21 SqlConnection Conn = new SqlConnection(strConn);
22 Conn.Open();
23 ad = new SqlDataAdapter(strSql,Conn);
24 ds = new DataSet();
25 ad.Fill(ds,"UserTest");
26 this.txtUserID.DataBindings.Add("Text",ds,"UserTest.UserID");
27 this.txtUserName.DataBindings.Add("Text",ds,"UserTest.UserName");
28 //Add方法选择的3个值的重载 第一个控件属性,第二个数据源,第三个字段名
29 this.bManager = this.BindingContext[ds,"UserTest"];
30 //BindingContext界面控件数据源给BindingManagerBase这个类,此类用于数据导航
31}
32private void btnDown_Click(object sender, System.EventArgs e)
33{
34 this.bManager.Position += 1;
35}
36private void btnUp_Click(object sender, System.EventArgs e)
37{
38 this.bManager.Position -= 1;
39}
40private void btnFirst_Click(object sender, System.EventArgs e)
41{
42 this.bManager.Position = 0;
43}
44private void btnLast_Click(object sender, System.EventArgs e)
45{
46 this.bManager.Position += this.ds.Tables["UserTest"].Rows.Count;
47}
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Data.SqlClient;
8private SqlDataAdapter ad;
9private DataSet ds;
10private BindingManagerBase bManager;//用于数据导航
11private System.Windows.Forms.Button btnFirst;
12private System.Windows.Forms.Button btnLast;
13private System.Windows.Forms.Button btnUp;
14private System.Windows.Forms.Button btnDown;
15private System.Windows.Forms.TextBox txtUserID;
16private System.Windows.Forms.TextBox txtUserName;
17private void Form1_Load(object sender, System.EventArgs e)
18{
19 string strConn = "Server=.; uid=sa; pwd=sa; database=Test";
20 string strSql = "Select UserID,UserName From UserTest";
21 SqlConnection Conn = new SqlConnection(strConn);
22 Conn.Open();
23 ad = new SqlDataAdapter(strSql,Conn);
24 ds = new DataSet();
25 ad.Fill(ds,"UserTest");
26 this.txtUserID.DataBindings.Add("Text",ds,"UserTest.UserID");
27 this.txtUserName.DataBindings.Add("Text",ds,"UserTest.UserName");
28 //Add方法选择的3个值的重载 第一个控件属性,第二个数据源,第三个字段名
29 this.bManager = this.BindingContext[ds,"UserTest"];
30 //BindingContext界面控件数据源给BindingManagerBase这个类,此类用于数据导航
31}
32private void btnDown_Click(object sender, System.EventArgs e)
33{
34 this.bManager.Position += 1;
35}
36private void btnUp_Click(object sender, System.EventArgs e)
37{
38 this.bManager.Position -= 1;
39}
40private void btnFirst_Click(object sender, System.EventArgs e)
41{
42 this.bManager.Position = 0;
43}
44private void btnLast_Click(object sender, System.EventArgs e)
45{
46 this.bManager.Position += this.ds.Tables["UserTest"].Rows.Count;
47}