using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class frmMain : Form
{
CurrencyManager cm;
public frmMain()
{
InitializeComponent();
#region List绑定
//建立一个集合,让它维持多个Person对象。
//List<Person> list = new List<Person>();
//list.Add(new Person("Caipeng", 32));
//list.Add(new Person("WangLiLi", 30));
//list.Add(new Person("Colin.Cai", 0));
//this.txtName.DataBindings.Add("Text", list, "Name");
//this.txtAge.DataBindings.Add("Text", list, "Age");
////增加以下的语句,获得管理集合的管理对象.
////下面的两个button演示cm如果管理集合的简单方法.
//cm = (CurrencyManager)this.BindingContext[list];
//this.txtName.DataBindings.Add("Text", dt, "Name");
//this.txtAge.DataBindings.Add("Text", dt, "Age");
#endregion
#region DataTable绑定
#region 构造数据
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("IsCCP", typeof(int));
for (int i = 0; i < 20; i++)
{
var dr = dt.NewRow();
dr["Name"] = "Name" + i;
dr["Age"] = i + 20;
dr["IsCCP"] = i % 3 == 0;
dt.Rows.Add(dr);
}
#endregion
for (int i = 0; i < dt.Columns.Count; i++) //应根据编码规范,写成通用方案
{
string columnName = dt.Columns[i].ColumnName;
foreach (Control item in this.Controls)
{
if (item.Name.Contains(columnName))//应根据编码规范,更严格的判断
{
switch (item.GetType().FullName)
{
case "System.Windows.Forms.Lable":
case "System.Windows.Forms.TextBox":
case "System.Windows.Forms.Button":
item.DataBindings.Add("Text", dt, columnName);
break;
case "System.Windows.Forms.CheckBox":
item.DataBindings.Add("Checked", dt, columnName);
break;
default:
break;
}
}
}
}
#endregion
cm = (CurrencyManager)this.BindingContext[dt];//获得管理集合的管理对象.
}
/// <summary>
/// 将当前的位置++
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
private void btnNext_Click(object sender, EventArgs e)
{
cm.Position++;
}
/// <summary>
/// 将当前的位置--
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
private void btnPrevious_Click(object sender, EventArgs e)
{
cm.Position--;
}
/// <summary>
/// 取得当前编辑对象
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
private void btnCurrent_Click(object sender, EventArgs e)
{
//var current = (Person)cm.List[cm.Position];//List对象绑定时
var current = (DataRowView)cm.List[cm.Position];
}
#region Designer
/// <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);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblName = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.lblAge = new System.Windows.Forms.Label();
this.txtAge = new System.Windows.Forms.TextBox();
this.btnPrevious = new System.Windows.Forms.Button();
this.btnNext = new System.Windows.Forms.Button();
this.btnCurrent = new System.Windows.Forms.Button();
this.chkIsCCP = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// lblName
//
this.lblName.AutoSize = true;
this.lblName.Location = new System.Drawing.Point(21, 36);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(41, 12);
this.lblName.TabIndex = 1;
this.lblName.Text = "姓名:";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(58, 31);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(100, 21);
this.txtName.TabIndex = 2;
//
// lblAge
//
this.lblAge.AutoSize = true;
this.lblAge.Location = new System.Drawing.Point(171, 32);
this.lblAge.Name = "lblAge";
this.lblAge.Size = new System.Drawing.Size(35, 12);
this.lblAge.TabIndex = 3;
this.lblAge.Text = "年龄:";
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(212, 29);
this.txtAge.Name = "txtAge";
this.txtAge.Size = new System.Drawing.Size(100, 21);
this.txtAge.TabIndex = 4;
//
// btnPrevious
//
this.btnPrevious.Location = new System.Drawing.Point(34, 73);
this.btnPrevious.Name = "btnPrevious";
this.btnPrevious.Size = new System.Drawing.Size(75, 23);
this.btnPrevious.TabIndex = 5;
this.btnPrevious.Text = "上一条";
this.btnPrevious.UseVisualStyleBackColor = true;
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
//
// btnNext
//
this.btnNext.Location = new System.Drawing.Point(139, 72);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(75, 23);
this.btnNext.TabIndex = 6;
this.btnNext.Text = "下一条";
this.btnNext.UseVisualStyleBackColor = true;
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// btnCurrent
//
this.btnCurrent.Location = new System.Drawing.Point(257, 72);
this.btnCurrent.Name = "btnCurrent";
this.btnCurrent.Size = new System.Drawing.Size(133, 23);
this.btnCurrent.TabIndex = 7;
this.btnCurrent.Text = "获取当前编辑对象";
this.btnCurrent.UseVisualStyleBackColor = true;
this.btnCurrent.Click += new System.EventHandler(this.btnCurrent_Click);
//
// chkIsCCP
//
this.chkIsCCP.AutoSize = true;
this.chkIsCCP.Location = new System.Drawing.Point(318, 32);
this.chkIsCCP.Name = "chkIsCCP";
this.chkIsCCP.Size = new System.Drawing.Size(72, 16);
this.chkIsCCP.TabIndex = 8;
this.chkIsCCP.Text = "是否党员";
this.chkIsCCP.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(523, 197);
this.Controls.Add(this.chkIsCCP);
this.Controls.Add(this.btnCurrent);
this.Controls.Add(this.btnNext);
this.Controls.Add(this.btnPrevious);
this.Controls.Add(this.txtAge);
this.Controls.Add(this.lblAge);
this.Controls.Add(this.txtName);
this.Controls.Add(this.lblName);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label lblAge;
private System.Windows.Forms.TextBox txtAge;
private System.Windows.Forms.Button btnPrevious;
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.Button btnCurrent;
private System.Windows.Forms.CheckBox chkIsCCP;
#endregion
}