The Flower of Evil
asp.net & XML Web Service

导航

 
代码:
  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7using System.Data.SqlClient;
  8
  9namespace MyBindingManager
 10{
 11    /// <summary>
 12    /// Form1 的摘要说明。
 13    /// </summary>

 14    public class Form1 : System.Windows.Forms.Form
 15    {
 16        private System.Windows.Forms.TextBox textBox1;
 17        private System.Windows.Forms.TextBox textBox2;
 18        private System.Windows.Forms.Button buttonBack;
 19        private System.Windows.Forms.Button buttonNext;
 20        private System.Data.DataSet dataSet1;
 21
 22        //Create a BindingManagerBase object with class scope
 23        BindingManagerBase bManager;
 24        /// <summary>
 25        /// 必需的设计器变量。
 26        /// </summary>

 27        private System.ComponentModel.Container components = null;
 28
 29        public Form1()
 30        {
 31            //
 32            // Windows 窗体设计器支持所必需的
 33            //
 34            InitializeComponent();
 35
 36            //
 37            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 38            //
 39        }

 40
 41        /// <summary>
 42        /// 清理所有正在使用的资源。
 43        /// </summary>

 44        protected override void Dispose( bool disposing )
 45        {
 46            if( disposing )
 47            {
 48                if (components != null
 49                {
 50                    components.Dispose();
 51                }

 52            }

 53            base.Dispose( disposing );
 54        }

 55
 56        Windows 窗体设计器生成的代码
122
123        /// <summary>
124        /// 应用程序的主入口点。
125        /// </summary>

126        [STAThread]
127        static void Main() 
128        {
129            Application.Run(new Form1());
130        }

131
132        private void Form1_Load(object sender, System.EventArgs e)
133        {
134            string ConStr=@"Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=SSPI";
135            string SQL="SELECT * FROM Employees";
136
137            SqlConnection conn = new SqlConnection(ConStr);
138            SqlDataAdapter da = new SqlDataAdapter(SQL,conn);
139            da.Fill(dataSet1,"Employees");
140
141            textBox1.DataBindings.Add("Text", dataSet1,"Employees.FirstName");
142            textBox2.DataBindings.Add("Text", dataSet1,"Employees.LastName");
143
144            bManager= this.BindingContext[dataSet1,"Employees"];
145        }

146
147        private void buttonNext_Click(object sender, System.EventArgs e)
148        {
149            bManager.Position+=1;
150        }

151
152        private void buttonBack_Click(object sender, System.EventArgs e)
153        {
154            bManager.Position-=1;
155        }

156    }

157}

158
  运行截图:
 Manager.gif

工作原理:
重点:
用类作用域声明 BindingManagerBase类对象之后,下一件要做的事情是根据窗体的BindingContext属性
得到合适的绑定管理器。可以在下面的代码中看到这点。
bManager=this.BindingContext[dataSet1,"Employees"];
"this"表示活动窗体对象,由根据其BindingContext属性得到适当的绑定管理器调用,为此,指定所需的
数据源和数据成员。这个例子中,BindingContext返回 CurrencyManager的实例,DataSet实现了IListSource
接口。有了绑定管理器,就可以管理Windows Form应用程序中不同控件在指定数据源上设置的所有数据
绑定。

实现导航按钮。每次单击“Next”按钮时都调用buttonNext_Click方法。该方法的体含有一行代码(如下所示),从本质上讲,这行代码使用前纪录在数据源中的位置增量,为此,对绑定管理器的Position属性返回的值进行增量:
bManager.Position +=1;
类似地,每次单击“Back”按钮时都调用buttonBack_Click方法。这个方法使用如下所示的一行代码,对当前纪录在数据源中的位置减量,为此,对绑定管理器的Position属性返回的值进行减量:
bManager.Position -=1;
posted on 2005-11-08 10:42  The Flower of Evil  阅读(433)  评论(0)    收藏  举报