SQL:见上 运行界面: 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 BindingDemoForm12 : Form ...{ // 資料集物件的類別層級建立 DataSet ds = new DataSet(); // 關聯性連結物件的類別層級宣告 DataRelation rel; // CurrencyManager 物件的類別層級宣告 BindingManagerBase bmCategory; public BindingDemoForm12() ...{ InitializeComponent(); } private void BindingDemoForm12_Load(object sender, System.EventArgs e) ...{ // 設定表單的最小大小 this.MinimumSize = new Size(680, 616); // 建立一個連接字串 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 產品編號, 產品, 供應商編號, 類別編號, 單位數量, 單價, 庫存量, 已訂購量, 安全存量, 不再銷售 FROM 產品資料"; // 將資料填入資料集內名稱為「產品資料」的資料表 myAD.Fill(ds, "產品資料"); // 宣告用來將資料集內之「產品類別」資料表連結至「產品資料」資料表的父欄位與子欄位 DataColumn parentcol; DataColumn childcol; parentcol = ds.Tables["產品類別"].Columns["類別編號"]; childcol = ds.Tables["產品資料"].Columns["類別編號"]; // 建立用來連結「產品類別」資料表與「產品資料」資料表的 DataRelation 物件,其名稱為「每一種類別的產品」。 rel = new DataRelation("每一種類別的產品", parentcol, childcol); ds.Relations.Add(rel); TextBoxCategoryID.DataBindings.Add("Text", ds, "產品類別.類別編號"); TextBoxCategoryName.DataBindings.Add("Text", ds, "產品類別.類別名稱"); TextBoxDescription.DataBindings.Add("Text", ds, "產品類別.說明"); // 將 DataGrid 控制項繫結至 DataRelation 物件(亦即「每一種類別的產品」) DataGridProduct.SetDataBinding(ds, "產品類別.每一種類別的產品"); // 取得代表「產品類別」資料表的 CurrencyManager 物件 bmCategory = this.BindingContext[ds, "產品類別"]; // 設定當引發 PositionChanged 事件時便執行事件處理常式 產品類別_PositionChanged bmCategory.PositionChanged += 產品類別_PositionChanged; // 設定資料記錄目前位置訊息的初值 TextBoxPosition.Text = string.Format("產品類別記錄:目前位置 {0} 總數 {1}", bmCategory.Position + 1, bmCategory.Count); myConnection.Close(); } protected void 產品類別_PositionChanged(object sender, System.EventArgs e) ...{ TextBoxPosition.Text = string.Format("產品類別記錄:目前位置 {0} 總數 {1}", bmCategory.Position + 1, bmCategory.Count); } // 按下「第一筆」按鈕 private void btnFirst_Click(object sender, System.EventArgs e) ...{ // 將 Position 屬性設定成 0 bmCategory.Position = 0; } // 按下「上一筆」按鈕 private void btnBack_Click(object sender, System.EventArgs e) ...{ if (bmCategory.Position > 0) ...{ // 將 Position 屬性遞減 1 bmCategory.Position -= 1; } } // 按下「下一筆」按鈕 private void btnNext_Click(object sender, System.EventArgs e) ...{ if (bmCategory.Position < bmCategory.Count - 1) ...{ // 將 Position 屬性遞增 1 bmCategory.Position += 1; } } // 按下「最後一筆」按鈕 private void btnEnd_Click(object sender, System.EventArgs e) ...{ bmCategory.Position = bmCategory.Count - 1; } }} 窗体代码: namespace ch1 ... { partial class BindingDemoForm12 ...{ /**//// <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() ...{ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BindingDemoForm12)); this.lblSupplierID = new System.Windows.Forms.Label(); this.lblProduct = new System.Windows.Forms.Label(); this.lblProductID = new System.Windows.Forms.Label(); this.TextBoxCategoryID = new System.Windows.Forms.TextBox(); this.TextBoxDescription = new System.Windows.Forms.TextBox(); this.TextBoxCategoryName = new System.Windows.Forms.TextBox(); 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.DataGridProduct = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.DataGridProduct)).BeginInit(); this.SuspendLayout(); // // lblSupplierID // this.lblSupplierID.AutoSize = true; this.lblSupplierID.Location = new System.Drawing.Point(149, 32); this.lblSupplierID.Name = "lblSupplierID"; this.lblSupplierID.Size = new System.Drawing.Size(35, 12); this.lblSupplierID.TabIndex = 102; this.lblSupplierID.Text = "說明:"; // // lblProduct // this.lblProduct.AutoSize = true; this.lblProduct.Location = new System.Drawing.Point(314, 8); this.lblProduct.Name = "lblProduct"; this.lblProduct.Size = new System.Drawing.Size(59, 12); this.lblProduct.TabIndex = 100; this.lblProduct.Text = "類別名稱:"; // // lblProductID // this.lblProductID.AutoSize = true; this.lblProductID.Location = new System.Drawing.Point(125, 8); this.lblProductID.Name = "lblProductID"; this.lblProductID.Size = new System.Drawing.Size(59, 12); this.lblProductID.TabIndex = 98; this.lblProductID.Text = "類別編號:"; // // TextBoxCategoryID // this.TextBoxCategoryID.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); this.TextBoxCategoryID.Location = new System.Drawing.Point(198, 5); this.TextBoxCategoryID.Name = "TextBoxCategoryID"; this.TextBoxCategoryID.Size = new System.Drawing.Size(100, 21); this.TextBoxCategoryID.TabIndex = 99; // // TextBoxDescription // this.TextBoxDescription.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); this.TextBoxDescription.Location = new System.Drawing.Point(198, 29); this.TextBoxDescription.Name = "TextBoxDescription"; this.TextBoxDescription.Size = new System.Drawing.Size(374, 21); this.TextBoxDescription.TabIndex = 103; // // TextBoxCategoryName // this.TextBoxCategoryName.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); this.TextBoxCategoryName.Location = new System.Drawing.Point(394, 5); this.TextBoxCategoryName.Name = "TextBoxCategoryName"; this.TextBoxCategoryName.Size = new System.Drawing.Size(177, 21); this.TextBoxCategoryName.TabIndex = 101; // // btnEnd // this.btnEnd.BackColor = System.Drawing.Color.White; this.btnEnd.Image = ((System.Drawing.Image)(resources.GetObject("btnEnd.Image"))); this.btnEnd.Location = new System.Drawing.Point(446, 97); this.btnEnd.Name = "btnEnd"; this.btnEnd.Size = new System.Drawing.Size(58, 21); this.btnEnd.TabIndex = 97; this.btnEnd.UseVisualStyleBackColor = false; this.btnEnd.Click += new System.EventHandler(this.btnEnd_Click); // // btnNext // this.btnNext.BackColor = System.Drawing.Color.White; this.btnNext.Image = ((System.Drawing.Image)(resources.GetObject("btnNext.Image"))); this.btnNext.Location = new System.Drawing.Point(366, 97); this.btnNext.Name = "btnNext"; this.btnNext.Size = new System.Drawing.Size(58, 21); this.btnNext.TabIndex = 96; this.btnNext.UseVisualStyleBackColor = false; this.btnNext.Click += new System.EventHandler(this.btnNext_Click); // // btnBack // this.btnBack.BackColor = System.Drawing.Color.White; this.btnBack.Image = ((System.Drawing.Image)(resources.GetObject("btnBack.Image"))); this.btnBack.Location = new System.Drawing.Point(286, 97); this.btnBack.Name = "btnBack"; this.btnBack.Size = new System.Drawing.Size(58, 21); this.btnBack.TabIndex = 95; this.btnBack.UseVisualStyleBackColor = false; this.btnBack.Click += new System.EventHandler(this.btnBack_Click); // // btnFirst // this.btnFirst.BackColor = System.Drawing.Color.White; this.btnFirst.Image = ((System.Drawing.Image)(resources.GetObject("btnFirst.Image"))); this.btnFirst.Location = new System.Drawing.Point(206, 97); this.btnFirst.Name = "btnFirst"; this.btnFirst.Size = new System.Drawing.Size(58, 21); this.btnFirst.TabIndex = 94; this.btnFirst.UseVisualStyleBackColor = false; 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(209, 63); this.TextBoxPosition.Name = "TextBoxPosition"; this.TextBoxPosition.Size = new System.Drawing.Size(290, 21); this.TextBoxPosition.TabIndex = 93; this.TextBoxPosition.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // DataGridProduct // this.DataGridProduct.AlternatingBackColor = System.Drawing.Color.Lavender; this.DataGridProduct.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.DataGridProduct.BackColor = System.Drawing.Color.WhiteSmoke; this.DataGridProduct.BackgroundColor = System.Drawing.Color.LightGray; this.DataGridProduct.CaptionBackColor = System.Drawing.Color.LightSteelBlue; this.DataGridProduct.CaptionForeColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.CaptionText = "產品基本資料"; this.DataGridProduct.DataMember = ""; this.DataGridProduct.FlatMode = true; this.DataGridProduct.Font = new System.Drawing.Font("Tahoma", 8F); this.DataGridProduct.ForeColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.GridLineColor = System.Drawing.Color.Gainsboro; this.DataGridProduct.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None; this.DataGridProduct.HeaderBackColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold); this.DataGridProduct.HeaderForeColor = System.Drawing.Color.WhiteSmoke; this.DataGridProduct.LinkColor = System.Drawing.Color.Teal; this.DataGridProduct.Location = new System.Drawing.Point(26, 127); this.DataGridProduct.Name = "DataGridProduct"; this.DataGridProduct.ParentRowsBackColor = System.Drawing.Color.Gainsboro; this.DataGridProduct.ParentRowsForeColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.SelectionBackColor = System.Drawing.Color.CadetBlue; this.DataGridProduct.SelectionForeColor = System.Drawing.Color.WhiteSmoke; this.DataGridProduct.Size = new System.Drawing.Size(610, 431); this.DataGridProduct.TabIndex = 92; // // BindingDemoForm12 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(648, 554); this.Controls.Add(this.lblSupplierID); this.Controls.Add(this.lblProduct); this.Controls.Add(this.lblProductID); this.Controls.Add(this.TextBoxCategoryID); this.Controls.Add(this.TextBoxDescription); this.Controls.Add(this.TextBoxCategoryName); 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.DataGridProduct); this.Name = "BindingDemoForm12"; this.Text = "BindingDemoForm12"; this.Load += new System.EventHandler(this.BindingDemoForm12_Load); ((System.ComponentModel.ISupportInitialize)(this.DataGridProduct)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion internal System.Windows.Forms.Label lblSupplierID; internal System.Windows.Forms.Label lblProduct; internal System.Windows.Forms.Label lblProductID; internal System.Windows.Forms.TextBox TextBoxCategoryID; internal System.Windows.Forms.TextBox TextBoxDescription; internal System.Windows.Forms.TextBox TextBoxCategoryName; 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.DataGrid DataGridProduct; }}