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 = ; i < ; i++)
{
var dr = dt.NewRow();
dr["Name"] = "Name" + i;
dr["Age"] = i + ;
dr["IsCCP"] = i % == ;
dt.Rows.Add(dr);
}
#endregion
for (int i = ; 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(, );
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(, );
this.lblName.TabIndex = ;
this.lblName.Text = "姓名:";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(, );
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(, );
this.txtName.TabIndex = ;
//
// lblAge
//
this.lblAge.AutoSize = true;
this.lblAge.Location = new System.Drawing.Point(, );
this.lblAge.Name = "lblAge";
this.lblAge.Size = new System.Drawing.Size(, );
this.lblAge.TabIndex = ;
this.lblAge.Text = "年龄:";
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(, );
this.txtAge.Name = "txtAge";
this.txtAge.Size = new System.Drawing.Size(, );
this.txtAge.TabIndex = ;
//
// btnPrevious
//
this.btnPrevious.Location = new System.Drawing.Point(, );
this.btnPrevious.Name = "btnPrevious";
this.btnPrevious.Size = new System.Drawing.Size(, );
this.btnPrevious.TabIndex = ;
this.btnPrevious.Text = "上一条";
this.btnPrevious.UseVisualStyleBackColor = true;
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
//
// btnNext
//
this.btnNext.Location = new System.Drawing.Point(, );
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(, );
this.btnNext.TabIndex = ;
this.btnNext.Text = "下一条";
this.btnNext.UseVisualStyleBackColor = true;
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// btnCurrent
//
this.btnCurrent.Location = new System.Drawing.Point(, );
this.btnCurrent.Name = "btnCurrent";
this.btnCurrent.Size = new System.Drawing.Size(, );
this.btnCurrent.TabIndex = ;
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(, );
this.chkIsCCP.Name = "chkIsCCP";
this.chkIsCCP.Size = new System.Drawing.Size(, );
this.chkIsCCP.TabIndex = ;
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(, );
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 }

WinFrom控件双向绑定的更多相关文章

  1. ASP.NET中后台数据和前台控件的绑定

    关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...

  2. WinForm开发-界面控件到实体,实体到界面控件自动绑定

    在WinForm开发中,我们是不是为绑定界面控件的数据而每个控件每个控件的赋值?在保存修改时是不是也是每个控件每个控件的赋值到实体中?字段一多,那简直就是噩梦.有没有像Web中那样方便的方法直接就自动 ...

  3. TreeView树形控件递归绑定数据库里的数据

    TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...

  4. asp.net学习之 数据绑定控件--表格绑定控件

    原文:asp.net学习之 数据绑定控件--表格绑定控件     数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...

  5. 在GridControl控件上绑定图片的几种操作方式

    我们知道,基于DevExpress的开发Winform的项目界面的时候,GridControl控件是经常用来绑定数据的,一般以常规的字符内容为主,有时候也会有图片的显示需要,那么如果显示图片,我们应该 ...

  6. ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定

    在众多表单元素中,有一类<select>元素用于绑定一组预定义列表.传统的ASP.NET Web Form中,它对应着一组重要的控件类型,即ListControl,我们经常用到DropDo ...

  7. 实现对DataGird控件的绑定操作

    //实现对DataGird控件的绑定操作 function InitGrid(queryData) { $('#grid').datagrid({ //定位到Table标签,Table标签的ID是gr ...

  8. WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定

    原文:WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定   WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM ...

  9. WPF 精修篇 WPF嵌入Winfrom控件

    原文:WPF 精修篇 WPF嵌入Winfrom控件 先增加DLL 支持 使用  WindowsFormsHost 来加载Forms的控件 引用命名空间 xmlns:forms="clr-na ...

随机推荐

  1. 003-基于impi zabbix监控r720 测试过程

    1.F2进入服务器bios 修改network  使这台服务器能够被远程访问. 2.在远程的centos 7 服务器上安装  impitool工具包 #ipmitool -I lanplus -H X ...

  2. Jmeter线程组间传递参数

    现在做测试和以前不太一样了,以前只要站在一个用户的角度做端到端的UI测试就可以了,现在不会做接口测试,出去都不好意思和别人打招呼.那提到接口测试,就不得不提一下接口测试利器Jmeter,大家也都知道, ...

  3. [易学易懂系列|rustlang语言|零基础|快速入门|(1)|开篇]

    今天我们来开一个新系列. 从零学习rustlang. 简单介绍下rustlang: Rust 是一门系统级编程语言,被设计为保证内存和线程安全,防止段错误产生.作为系统级编程语言,它的基本理念是 “零 ...

  4. 深入理解JAVA虚拟机 自动内存管理机制

    运行时数据区域 其中右侧三个一起的部分是每个线程一份,左侧两个是所有线程共享的. 程序计数器(Program Counter Register) 英文名称叫Program Counter Regist ...

  5. DirectX屏幕捕获和输出视频

    #include <Windows.h> #include <mfapi.h> #include <mfidl.h> #include <Mfreadwrit ...

  6. centos6.5下修改系统的roo用户/非root用户的密码

    1.修改系统root用户的密码 [........~]# passwd然后输入新密码,若提示密码太简单,无需理会,直接敲回车: 然后再次输入新密码,即可修改成功. 2.修改系统非root用户的密码:e ...

  7. Redis如何实现高可用【主从复制+哨兵机制+keepalived】

    实现redis高可用机制的一些方法: 保证redis高可用机制需要redis主从复制.redis持久化机制.哨兵机制.keepalived等的支持. 主从复制的作用:数据备份.读写分离.分布式集群.实 ...

  8. Harmonic Number (II)

    Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I ...

  9. vue之Object.defineProperty()

    了解Object.defineProerty()方法 关于Object.defineProperty()方法的解释,理解Object.defineProperty的作用 这篇文章做了很详细的概述 关于 ...

  10. python学习之路(23)

    类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各 ...