ado.net c#.net2005 From第一讲(BindingDemoForm13)

来源:互联网 发布:pc淘宝视频管理入口 编辑:程序博客网 时间:2024/05/21 10:01

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(736615);

            
// 建立一個連接字串
            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 窗体设计器生成的代码

        
internal System.Windows.Forms.DataGrid DataGridCategory;
        
internal System.Windows.Forms.Label Label1;
        
internal System.Windows.Forms.DataGrid DataGridProduct;
    }

}
原创粉丝点击