数据库: CREATE TABLE [ 人員健康 ] ( [ 員工編號 ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL , [ 姓名 ] [ nvarchar ] ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL , [ 性別 ] [ bit ] NOT NULL CONSTRAINT [ DF__人員健康__性別__0880433F ] DEFAULT ( 0 ), [ 出生日期 ] [ datetime ] NULL , [ 血型 ] [ nvarchar ] ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL , CONSTRAINT [ aaaaa人員健康_PK ] PRIMARY KEY NONCLUSTERED ( [ 員工編號 ] ) ON [ PRIMARY ] ) ON [ PRIMARY ] GO CREATE TABLE [ BloodType ] ( [ Id ] [ smallint ] NOT NULL , [ Type ] [ nvarchar ] ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL , [ Characteristic ] [ nvarchar ] ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL , CONSTRAINT [ PK_BloodType ] PRIMARY KEY CLUSTERED ( [ Id ] ) ON [ PRIMARY ] ) ON [ PRIMARY ] GO 运行界面: cs代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace ch1 ... { public partial class BindingDemoForm7 : Form ...{ public BindingDemoForm7() ...{ InitializeComponent(); } // 資料集物件的類別層級建立 DataSet ds = new DataSet(); // CurrencyManager 物件的類別層級宣告 BindingManagerBase bmEmployeeHealth; private void BindingDemoForm7_Load(object sender, System.EventArgs e) ...{ // 設定表單的最小大小 this.MinimumSize = new Size(627, 457); // 建立一個連接字串 string strConnection = "Server=(local);Database=ch1;uid=sa;pwd="; // 建立一個查詢命令字串 string strSql = "SELECT 員工編號,姓名,性別,出生日期,血型 FROM 人員健康"; // 建立一個資料連接 SqlConnection myConnection = new SqlConnection(strConnection); // 建立一個資料配接器以便針對資料來源執行 SELECT 陳述式來提取出要填入資料集的資料記錄 SqlDataAdapter myAD = new SqlDataAdapter(strSql, myConnection); // 將資料填入資料集內名稱為「人員健康」的資料表 myAD.Fill(ds, "人員健康"); // 重新指定用來提取資料來源之資料記錄的 SELECT 陳述式 myAD.SelectCommand.CommandText = "SELECT Type, Characteristic FROM BloodType"; // 將資料填入資料集內名稱為 BloodType 的資料表 myAD.Fill(ds, "BloodType"); // 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「人員健康」資料表的「員工編號」欄位 TextBoxID.DataBindings.Add("Text", ds, "人員健康.員工編號"); // 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「人員健康」資料表的「姓名」欄位 TextBoxName.DataBindings.Add("Text", ds, "人員健康.姓名"); // 將 CheckBox 控制項的 Checked 屬性繫結至資料集 ds 內之「人員健康」資料表的「性別」欄位 CheckBoxGender.DataBindings.Add("Checked", ds, "人員健康.性別"); // 將 DateTimePicker 控制項的 Value 屬性繫結至資料集 ds 內之「人員健康」資料表的「出生日期」欄位 DateTimePickerBirthday.DataBindings.Add("Value", ds, "人員健康.出生日期"); // 將 ComboBox 控制項的資料來源設定成資料集 ds 內的 BloodType 資料表 ComboBoxBloodType.DataSource = ds.Tables["BloodType"]; // 將 ComboBox 控制項的 DisplayMember 屬性設定成 BloodType 資料表的 Characteristic 欄位 ComboBoxBloodType.DisplayMember = "Characteristic"; // 將 ComboBox 控制項的 ValueMember 屬性設定成 BloodType 資料表的 Type 欄位 ComboBoxBloodType.ValueMember = "Type"; // 將 SelectedValue 屬性繫結至「人員健康」資料表的「血型」欄位,如此一來, // 使用者所選取之選項的 Type 欄位內容便會寫入「人員健康」資料表的「血型」欄位。 ComboBoxBloodType.DataBindings.Add("SelectedValue", ds, "人員健康.血型"); // 取得代表「人員健康」資料表的 CurrencyManager 物件 bmEmployeeHealth = this.BindingContext[ds, "人員健康"]; // 設定當引發 PositionChanged 事件時便執行事件處理常式 人員健康_PositionChanged bmEmployeeHealth.PositionChanged += 人員健康_PositionChanged; // 設定資料記錄目前位置訊息的初值 TextBoxPosition.Text = string.Format("資料記錄:目前位置 {0} 總數 {1}", bmEmployeeHealth.Position + 1, bmEmployeeHealth.Count); // 關閉對資料庫的連接 myConnection.Close(); } // 更新資料記錄目前位置的訊息 protected void 人員健康_PositionChanged(object sender, System.EventArgs e) ...{ TextBoxPosition.Text = string.Format("資料記錄:目前位置 {0} 總數 {1}", bmEmployeeHealth.Position + 1, bmEmployeeHealth.Count); } // 按下「第一筆」按鈕 private void btnFirst_Click(object sender, System.EventArgs e) ...{ // 將 Position 屬性設定成 0 bmEmployeeHealth.Position = 0; } // 按下「上一筆」按鈕 private void btnBack_Click(object sender, System.EventArgs e) ...{ if (bmEmployeeHealth.Position > 0) ...{ // 將 Position 屬性遞減 1 bmEmployeeHealth.Position -= 1; } } // 按下「下一筆」按鈕 private void btnNext_Click(object sender, System.EventArgs e) ...{ if (bmEmployeeHealth.Position < bmEmployeeHealth.Count - 1) ...{ // 將 Position 屬性遞增 1 bmEmployeeHealth.Position += 1; } } // 按下「最後一筆」按鈕 private void btnEnd_Click(object sender, System.EventArgs e) ...{ bmEmployeeHealth.Position = bmEmployeeHealth.Count - 1; } }} 窗体代码: namespace ch1 ... { partial class BindingDemoForm7 ...{ /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) ...{ if (disposing && (components != null)) ...{ components.Dispose(); } base.Dispose(disposing); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() ...{ this.ComboBoxBloodType = new System.Windows.Forms.ComboBox(); this.lblHeader = new System.Windows.Forms.Label(); this.btnEnd = new System.Windows.Forms.Button(); this.btnNext = new System.Windows.Forms.Button(); this.btnBack = new System.Windows.Forms.Button(); this.btnFirst = new System.Windows.Forms.Button(); this.TextBoxPosition = new System.Windows.Forms.TextBox(); this.DateTimePickerBirthday = new System.Windows.Forms.DateTimePicker(); this.CheckBoxGender = new System.Windows.Forms.CheckBox(); this.lblBloodType = new System.Windows.Forms.Label(); this.lblBirthday = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.lblName = new System.Windows.Forms.Label(); this.lblID = new System.Windows.Forms.Label(); this.TextBoxName = new System.Windows.Forms.TextBox(); this.TextBoxID = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // ComboBoxBloodType // this.ComboBoxBloodType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.ComboBoxBloodType.Location = new System.Drawing.Point(196, 154); this.ComboBoxBloodType.Name = "ComboBoxBloodType"; this.ComboBoxBloodType.Size = new System.Drawing.Size(320, 20); this.ComboBoxBloodType.TabIndex = 76; // // lblHeader // this.lblHeader.AutoSize = true; this.lblHeader.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136))); this.lblHeader.ForeColor = System.Drawing.Color.Red; this.lblHeader.Location = new System.Drawing.Point(12, 19); this.lblHeader.Name = "lblHeader"; this.lblHeader.Size = new System.Drawing.Size(506, 16); this.lblHeader.TabIndex = 75; this.lblHeader.Text = "使用 ComboBox 控制項來編輯「人員健康」資料表的「血型」欄位"; // // btnEnd // this.btnEnd.Location = new System.Drawing.Point(403, 297); this.btnEnd.Name = "btnEnd"; this.btnEnd.Size = new System.Drawing.Size(75, 21); this.btnEnd.TabIndex = 74; this.btnEnd.Text = "最後一筆"; this.btnEnd.Click += new System.EventHandler(this.btnEnd_Click); // // btnNext // this.btnNext.Location = new System.Drawing.Point(314, 297); this.btnNext.Name = "btnNext"; this.btnNext.Size = new System.Drawing.Size(75, 21); this.btnNext.TabIndex = 73; this.btnNext.Text = "下一筆"; this.btnNext.Click += new System.EventHandler(this.btnNext_Click); // // btnBack // this.btnBack.Location = new System.Drawing.Point(225, 297); this.btnBack.Name = "btnBack"; this.btnBack.Size = new System.Drawing.Size(75, 21); this.btnBack.TabIndex = 72; this.btnBack.Text = "上一筆"; this.btnBack.Click += new System.EventHandler(this.btnBack_Click); // // btnFirst // this.btnFirst.Location = new System.Drawing.Point(136, 297); this.btnFirst.Name = "btnFirst"; this.btnFirst.Size = new System.Drawing.Size(75, 21); this.btnFirst.TabIndex = 71; this.btnFirst.Text = "第一筆"; this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click); // // TextBoxPosition // this.TextBoxPosition.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.TextBoxPosition.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.TextBoxPosition.Location = new System.Drawing.Point(142, 262); this.TextBoxPosition.Name = "TextBoxPosition"; this.TextBoxPosition.Size = new System.Drawing.Size(333, 21); this.TextBoxPosition.TabIndex = 70; this.TextBoxPosition.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // DateTimePickerBirthday // this.DateTimePickerBirthday.Location = new System.Drawing.Point(196, 127); this.DateTimePickerBirthday.Name = "DateTimePickerBirthday"; this.DateTimePickerBirthday.Size = new System.Drawing.Size(200, 21); this.DateTimePickerBirthday.TabIndex = 68; // // CheckBoxGender // this.CheckBoxGender.Location = new System.Drawing.Point(196, 103); this.CheckBoxGender.Name = "CheckBoxGender"; this.CheckBoxGender.Size = new System.Drawing.Size(16, 19); this.CheckBoxGender.TabIndex = 66; // // lblBloodType // this.lblBloodType.AutoSize = true; this.lblBloodType.Location = new System.Drawing.Point(147, 155); this.lblBloodType.Name = "lblBloodType"; this.lblBloodType.Size = new System.Drawing.Size(35, 12); this.lblBloodType.TabIndex = 69; this.lblBloodType.Text = "血型:"; // // lblBirthday // this.lblBirthday.AutoSize = true; this.lblBirthday.Location = new System.Drawing.Point(123, 129); this.lblBirthday.Name = "lblBirthday"; this.lblBirthday.Size = new System.Drawing.Size(59, 12); this.lblBirthday.TabIndex = 67; this.lblBirthday.Text = "出生日期:"; // // Label1 // this.Label1.AutoSize = true; this.Label1.Location = new System.Drawing.Point(147, 106); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(35, 12); this.Label1.TabIndex = 65; this.Label1.Text = "性別:"; // // lblName // this.lblName.AutoSize = true; this.lblName.Location = new System.Drawing.Point(147, 83); this.lblName.Name = "lblName"; this.lblName.Size = new System.Drawing.Size(35, 12); this.lblName.TabIndex = 63; this.lblName.Text = "姓名:"; // // lblID // this.lblID.AutoSize = true; this.lblID.Location = new System.Drawing.Point(123, 56); this.lblID.Name = "lblID"; this.lblID.Size = new System.Drawing.Size(59, 12); this.lblID.TabIndex = 61; this.lblID.Text = "員工編號:"; // // TextBoxName // this.TextBoxName.Location = new System.Drawing.Point(196, 81); this.TextBoxName.Name = "TextBoxName"; this.TextBoxName.Size = new System.Drawing.Size(100, 21); this.TextBoxName.TabIndex = 64; // // TextBoxID // this.TextBoxID.Location = new System.Drawing.Point(196, 53); this.TextBoxID.Name = "TextBoxID"; this.TextBoxID.Size = new System.Drawing.Size(100, 21); this.TextBoxID.TabIndex = 62; // // BindingDemoForm7 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(545, 440); this.Controls.Add(this.ComboBoxBloodType); this.Controls.Add(this.lblHeader); this.Controls.Add(this.btnEnd); this.Controls.Add(this.btnNext); this.Controls.Add(this.btnBack); this.Controls.Add(this.btnFirst); this.Controls.Add(this.TextBoxPosition); this.Controls.Add(this.DateTimePickerBirthday); this.Controls.Add(this.CheckBoxGender); this.Controls.Add(this.lblBloodType); this.Controls.Add(this.lblBirthday); this.Controls.Add(this.Label1); this.Controls.Add(this.lblName); this.Controls.Add(this.lblID); this.Controls.Add(this.TextBoxName); this.Controls.Add(this.TextBoxID); this.Name = "BindingDemoForm7"; this.Text = "BindingDemoForm7"; this.Load += new System.EventHandler(this.BindingDemoForm7_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion internal System.Windows.Forms.ComboBox ComboBoxBloodType; internal System.Windows.Forms.Label lblHeader; internal System.Windows.Forms.Button btnEnd; internal System.Windows.Forms.Button btnNext; internal System.Windows.Forms.Button btnBack; internal System.Windows.Forms.Button btnFirst; internal System.Windows.Forms.TextBox TextBoxPosition; internal System.Windows.Forms.DateTimePicker DateTimePickerBirthday; internal System.Windows.Forms.CheckBox CheckBoxGender; internal System.Windows.Forms.Label lblBloodType; internal System.Windows.Forms.Label lblBirthday; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.Label lblName; internal System.Windows.Forms.Label lblID; internal System.Windows.Forms.TextBox TextBoxName; internal System.Windows.Forms.TextBox TextBoxID; }}