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

运行界面:
 

cs代码:

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  ch1
{
    
public partial class BindingDemoForm14 : Form
    
{
        
// DataTable 物件的類別層級建立
        DataTable colorSourceTable = MakeColorTable();
        
public BindingDemoForm14()
        
{
            InitializeComponent();
        }


        
private void BindingDemoForm14_Load(object sender, System.EventArgs e)
        
{
            BindControlProperties();
        }


        
private void BindControlProperties()
        
{
            
// 將 ComboBox 控制項繫結至 colorSourceTable 資料表的「色彩描述」欄位
            ComboBoxColor.DataSource = colorSourceTable;
            ComboBoxColor.DisplayMember 
= "色彩描述";

            
// 在加入新的 Binding 物件之前先移除集合內所有的 Binding 物件
            TextBox1.DataBindings.Clear();
            
// 將 TextBox 控制項的 Text、BackColor 與 ForeColor 屬性繫結至 DataTable 的欄位
            TextBox1.DataBindings.Add("Text", colorSourceTable, "色彩描述");
            TextBox1.DataBindings.Add(
"BackColor", colorSourceTable, "背景色");
            TextBox1.DataBindings.Add(
"ForeColor", colorSourceTable, "前景色");

            
// 在加入新的 Binding 物件之前先移除集合內所有的 Binding 物件
            lblFormCaption.DataBindings.Clear();
            
// 將 Label 控制項的 Text 屬性繫結至表單的 Text 屬性
            lblFormCaption.DataBindings.Add("Text"this"Text");
            
// 將 Label 控制項的 BackColor 屬性繫結至 TextBox 控制項的 ForeColor 屬性
            lblFormCaption.DataBindings.Add("BackColor", TextBox1, "ForeColor");
            
// 將 Label 控制項的 ForeColor 屬性繫結至 TextBox 控制項的 BackColor 屬性
            lblFormCaption.DataBindings.Add("ForeColor", TextBox1, "BackColor");
        }


        
private static DataTable MakeColorTable()
        
{

            
// 建立一個內含三個欄位的 DataTable 物件, 其中兩個欄位內含 Color 物件。
            DataTable colorSourceTable = new DataTable("colorSourceTable");
            colorSourceTable.Columns.Add(
"色彩描述");
            colorSourceTable.Columns.Add(
"背景色"typeof(Color));
            colorSourceTable.Columns.Add(
"前景色"typeof(Color));

            
// 於資料表中加入三筆資料記錄
            DataRow myDataRow;

            myDataRow 
= colorSourceTable.NewRow();
            myDataRow[
"色彩描述"= "紅底黃字";
            myDataRow[
"背景色"= Color.Red;
            myDataRow[
"前景色"= Color.Yellow;
            colorSourceTable.Rows.Add(myDataRow);

            myDataRow 
= colorSourceTable.NewRow();
            myDataRow[
"色彩描述"= "白底藍字";
            myDataRow[
"背景色"= Color.White;
            myDataRow[
"前景色"= Color.Blue;
            colorSourceTable.Rows.Add(myDataRow);

            myDataRow 
= colorSourceTable.NewRow();
            myDataRow[
"色彩描述"= "紫底水藍字";
            myDataRow[
"背景色"= Color.Purple;
            myDataRow[
"前景色"= Color.Cyan;
            colorSourceTable.Rows.Add(myDataRow);

            
return colorSourceTable;
        }

    }

}

窗体代码:

 

namespace  ch1
{
    
partial class BindingDemoForm14
    
{
        
/// <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.Label Label2;
        
internal System.Windows.Forms.Label lblFormCaption;
        
internal System.Windows.Forms.ComboBox ComboBoxColor;
        
internal System.Windows.Forms.TextBox TextBox1;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值