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 BindingDemoForm13 : Form ...{ // 資料集物件的類別層級建立 DataSet ds = new DataSet(); // 關聯性連結物件的類別層級宣告 DataRelation rel; public BindingDemoForm13() ...{ InitializeComponent(); } private void BindingDemoForm13_Load(object sender, System.EventArgs e) ...{ // 設定表單的最小大小 this.MinimumSize = new Size(736, 615); // 建立一個連接字串 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); // 將顯示產品類別的 DataGrid 控制項繫結至「產品類別」資料表 DataGridCategory.SetDataBinding(ds, "產品類別"); // 將顯示特定產品類別之產品資料的 DataGrid 控制項繫結至 DataRelation 物件(亦即「每一種類別的產品」) DataGridProduct.SetDataBinding(ds, "產品類別.每一種類別的產品"); // 設定 DataGrid 控制項的外觀格式 this.CreateCategoryColumnStyle(); myConnection.Close(); } private void CreateCategoryColumnStyle() ...{ // 建立一個 GridTableStyle 物件並將其 MappingName // 屬性設定成資料表的名稱 DataGridTableStyle TSCategory = new DataGridTableStyle(); TSCategory.MappingName = "產品類別"; TSCategory.AlternatingBackColor = System.Drawing.Color.Lavender; TSCategory.BackColor = System.Drawing.Color.WhiteSmoke; // 加入一個 GridColumnStyle 並將 MappingName // 屬性設定成您所要顯示的資料表欄位。 DataGridTextBoxColumn TCId = new DataGridTextBoxColumn(); TCId.MappingName = "類別編號"; TCId.HeaderText = "類別編號"; TCId.Width = 100; TCId.Alignment = HorizontalAlignment.Center; TCId.ReadOnly = true; TSCategory.GridColumnStyles.Add(TCId); DataGridTextBoxColumn TCName = new DataGridTextBoxColumn(); TCName.MappingName = "類別名稱"; TCName.HeaderText = "類別名稱"; TCName.Width = 120; TSCategory.GridColumnStyles.Add(TCName); DataGridTextBoxColumn TCDescription = new DataGridTextBoxColumn(); TCDescription.MappingName = "說明"; TCDescription.HeaderText = "說明"; TCDescription.Width = 230; TSCategory.GridColumnStyles.Add(TCDescription); // 將 DataGridTableStyle 執行個體加至 GridTableStylesCollection 中 DataGridCategory.TableStyles.Add(TSCategory); } }} 窗体代码: namespace ch1 ... { partial class BindingDemoForm13 ...{ /**//// <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.DataGridCategory = new System.Windows.Forms.DataGrid(); this.Label1 = new System.Windows.Forms.Label(); this.DataGridProduct = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.DataGridCategory)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.DataGridProduct)).BeginInit(); this.SuspendLayout(); // // DataGridCategory // this.DataGridCategory.AllowNavigation = false; this.DataGridCategory.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.DataGridCategory.CaptionText = "產品類別"; this.DataGridCategory.DataMember = ""; this.DataGridCategory.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.DataGridCategory.Location = new System.Drawing.Point(146, 37); this.DataGridCategory.Name = "DataGridCategory"; this.DataGridCategory.Size = new System.Drawing.Size(507, 155); this.DataGridCategory.TabIndex = 59; // // Label1 // this.Label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.Label1.Font = new System.Drawing.Font("PMingLiU", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136))); this.Label1.ForeColor = System.Drawing.Color.Red; this.Label1.Location = new System.Drawing.Point(146, -4); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(507, 37); this.Label1.TabIndex = 58; this.Label1.Text = "DataGrid 對 DataGrid 的一對多介面"; this.Label1.TextAlign = System.Drawing.ContentAlignment.BottomCenter; // // 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(40, 202); 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(718, 359); this.DataGridProduct.TabIndex = 57; // // BindingDemoForm13 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(799, 556); this.Controls.Add(this.DataGridCategory); this.Controls.Add(this.Label1); this.Controls.Add(this.DataGridProduct); this.Name = "BindingDemoForm13"; this.Text = "BindingDemoForm13"; this.Load += new System.EventHandler(this.BindingDemoForm13_Load); ((System.ComponentModel.ISupportInitialize)(this.DataGridCategory)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.DataGridProduct)).EndInit(); this.ResumeLayout(false); } #endregion internal System.Windows.Forms.DataGrid DataGridCategory; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.DataGrid DataGridProduct; }}