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

来源:互联网 发布:校园网络文化活动方案 编辑:程序博客网 时间:2024/06/07 13:28

数据库:

CREATE TABLE [學生] (
    
[身份證字號] [nvarchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
    
[學生姓名] [nvarchar] (12) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
[性別] [bit] NOT NULL CONSTRAINT [DF__學生__性別__3A179ED3] DEFAULT (0),
    
[家長姓名] [nvarchar] (12) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
[住家地址] [nvarchar] (60) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
[郵遞區號] [nvarchar] (5) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
[電話號碼] [nvarchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
[出生日期] [datetime] NULL ,
    
[身高] [real] NULL CONSTRAINT [DF__學生__身高__3B0BC30C] DEFAULT (0),
    
[體重] [real] NULL CONSTRAINT [DF__學生__體重__3BFFE745] DEFAULT (0),
    
[血型] [nvarchar] (3) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
[自傳] [ntext] COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    
CONSTRAINT [學生_PK] PRIMARY KEY  NONCLUSTERED 
    (
        
[身份證字號]
    )  
ON [PRIMARY] 
ON [PRIMARY] TEXTIMAGE_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 BindingDemoForm10 : Form
    
{
        
// 資料集物件的類別層級建立
        DataSet ds = new DataSet();
        
public BindingDemoForm10()
        
{
            InitializeComponent();
        }


        
private void BindingDemoForm10_Load(object sender, System.EventArgs e)
        
{
            
// 設定表單的最小大小
            this.MinimumSize = new Size(696462);
            
// 建立一個連接字串
            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, "學生");

            myConnection.Close();

            
// 將 DataGrid 控制項繫結至 ds 資料集的「學生」資料表
            DataGridStudent.SetDataBinding(ds, "學生");

            
// 設定 DataGrid 控制項的外觀格式
            this.CreateStudentColumnStyle();


        }


        
private void CreateStudentColumnStyle()
        
{
            
// 建立一個 GridTableStyle 物件並將其 MappingName 
            
// 屬性設定成資料表的名稱
            DataGridTableStyle TSStudents = new DataGridTableStyle();
            TSStudents.MappingName 
= "學生";
            TSStudents.PreferredRowHeight 
= 40;
            TSStudents.AlternatingBackColor 
= System.Drawing.Color.Lavender;
            TSStudents.BackColor 
= System.Drawing.Color.WhiteSmoke;

            
// 加入一個 GridColumnStyle 並將 MappingName 
            
// 屬性設定成您所要顯示的資料表欄位。 
            DataGridTextBoxColumn TCId = new DataGridTextBoxColumn();
            TCId.MappingName 
= "身份證字號";
            TCId.HeaderText 
= "身份證字號";
            TCId.Width 
= 90;
            TSStudents.GridColumnStyles.Add(TCId);

            DataGridTextBoxColumn TCName 
= new DataGridTextBoxColumn();
            TCName.MappingName 
= "學生姓名";
            TCName.HeaderText 
= "學生姓名";
            TCName.Width 
= 70;
            TSStudents.GridColumnStyles.Add(TCName);

            DataGridBoolColumn TCGender 
= new DataGridBoolColumn();
            TCGender.MappingName 
= "性別";
            TCGender.HeaderText 
= "性別";
            TCGender.Width 
= 40;
            TSStudents.GridColumnStyles.Add(TCGender);

            DataGridTextBoxColumn TCParentName 
= new DataGridTextBoxColumn();
            TCParentName.MappingName 
= "家長姓名";
            TCParentName.HeaderText 
= "家長姓名";
            TCParentName.Width 
= 70;
            TSStudents.GridColumnStyles.Add(TCParentName);

            DataGridTextBoxColumn TCAddress 
= new DataGridTextBoxColumn();
            TCAddress.MappingName 
= "住家地址";
            TCAddress.HeaderText 
= "住家地址";
            TCAddress.Width 
= 120;
            TCAddress.TextBox.Multiline 
= true;
            TCAddress.TextBox.ScrollBars 
= ScrollBars.Vertical;
            TSStudents.GridColumnStyles.Add(TCAddress);

            DataGridTextBoxColumn TCZip 
= new DataGridTextBoxColumn();
            TCZip.MappingName 
= "郵遞區號";
            TCZip.HeaderText 
= "郵遞區號";
            TCZip.Width 
= 70;
            TSStudents.GridColumnStyles.Add(TCZip);

            DataGridTextBoxColumn TCPhone 
= new DataGridTextBoxColumn();
            TCPhone.MappingName 
= "電話號碼";
            TCPhone.HeaderText 
= "電話號碼";
            TCPhone.Width 
= 70;
            TSStudents.GridColumnStyles.Add(TCPhone);

            DataGridTextBoxColumn TCHeight 
= new DataGridTextBoxColumn();
            TCHeight.MappingName 
= "身高";
            TCHeight.HeaderText 
= "身高";
            TCHeight.Width 
= 40;
            TSStudents.GridColumnStyles.Add(TCHeight);

            DataGridTextBoxColumn TCWeight 
= new DataGridTextBoxColumn();
            TCWeight.MappingName 
= "體重";
            TCWeight.HeaderText 
= "體重";
            TCWeight.Width 
= 40;
            TSStudents.GridColumnStyles.Add(TCWeight);

            DataGridTextBoxColumn TCBloodType 
= new DataGridTextBoxColumn();
            TCBloodType.MappingName 
= "血型";
            TCBloodType.HeaderText 
= "血型";
            TCBloodType.Width 
= 40;
            TSStudents.GridColumnStyles.Add(TCBloodType);

            DataGridTextBoxColumn TCContent 
= new DataGridTextBoxColumn();
            TCContent.MappingName 
= "自傳";
            TCContent.HeaderText 
= "自傳";
            TCContent.Width 
= 200;
            TCContent.TextBox.Multiline 
= true;
            TCContent.TextBox.ScrollBars 
= ScrollBars.Vertical;
            TSStudents.GridColumnStyles.Add(TCContent);

            
// 將 DataGridTableStyle 執行個體加至 GridTableStylesCollection 中
            DataGridStudent.TableStyles.Add(TSStudents);
        }

    }

}

窗体代码:

 

namespace ch1
{
    
partial class BindingDemoForm10
    
{
        
/// <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 DataGridStudent;
    }

}
原创粉丝点击