1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. public partial class frmMain : Form
  10. {
  11. CurrencyManager cm;
  12. public frmMain()
  13. {
  14. InitializeComponent();
  15. #region List绑定
  16. //建立一个集合,让它维持多个Person对象。
  17. //List<Person> list = new List<Person>();
  18. //list.Add(new Person("Caipeng", 32));
  19. //list.Add(new Person("WangLiLi", 30));
  20. //list.Add(new Person("Colin.Cai", 0));
  21. //this.txtName.DataBindings.Add("Text", list, "Name");
  22. //this.txtAge.DataBindings.Add("Text", list, "Age");
  23. ////增加以下的语句,获得管理集合的管理对象.
  24. ////下面的两个button演示cm如果管理集合的简单方法.
  25. //cm = (CurrencyManager)this.BindingContext[list];
  26. //this.txtName.DataBindings.Add("Text", dt, "Name");
  27. //this.txtAge.DataBindings.Add("Text", dt, "Age");
  28. #endregion
  29. #region DataTable绑定
  30. #region 构造数据
  31. DataTable dt = new DataTable();
  32. dt.Columns.Add("Name", typeof(string));
  33. dt.Columns.Add("Age", typeof(int));
  34. dt.Columns.Add("IsCCP", typeof(int));
  35. for (int i = ; i < ; i++)
  36. {
  37. var dr = dt.NewRow();
  38. dr["Name"] = "Name" + i;
  39. dr["Age"] = i + ;
  40. dr["IsCCP"] = i % == ;
  41. dt.Rows.Add(dr);
  42. }
  43. #endregion
  44. for (int i = ; i < dt.Columns.Count; i++) //应根据编码规范,写成通用方案
  45. {
  46. string columnName = dt.Columns[i].ColumnName;
  47. foreach (Control item in this.Controls)
  48. {
  49. if (item.Name.Contains(columnName))//应根据编码规范,更严格的判断
  50. {
  51. switch (item.GetType().FullName)
  52. {
  53. case "System.Windows.Forms.Lable":
  54. case "System.Windows.Forms.TextBox":
  55. case "System.Windows.Forms.Button":
  56. item.DataBindings.Add("Text", dt, columnName);
  57. break;
  58. case "System.Windows.Forms.CheckBox":
  59. item.DataBindings.Add("Checked", dt, columnName);
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. #endregion
  68. cm = (CurrencyManager)this.BindingContext[dt];//获得管理集合的管理对象.
  69. }
  70. /// <summary>
  71. /// 将当前的位置++
  72. /// </summary>
  73. /// <param name="sender">sender</param>
  74. /// <param name="e">e</param>
  75. private void btnNext_Click(object sender, EventArgs e)
  76. {
  77. cm.Position++;
  78. }
  79. /// <summary>
  80. /// 将当前的位置--
  81. /// </summary>
  82. /// <param name="sender">sender</param>
  83. /// <param name="e">e</param>
  84. private void btnPrevious_Click(object sender, EventArgs e)
  85. {
  86. cm.Position--;
  87. }
  88. /// <summary>
  89. /// 取得当前编辑对象
  90. /// </summary>
  91. /// <param name="sender">sender</param>
  92. /// <param name="e">e</param>
  93. private void btnCurrent_Click(object sender, EventArgs e)
  94. {
  95. //var current = (Person)cm.List[cm.Position];//List对象绑定时
  96. var current = (DataRowView)cm.List[cm.Position];
  97. }
  98.  
  99. #region Designer
  100.  
  101. /// <summary>
  102. /// 必需的设计器变量。
  103. /// </summary>
  104. private System.ComponentModel.IContainer components = null;
  105.  
  106. /// <summary>
  107. /// 清理所有正在使用的资源。
  108. /// </summary>
  109. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  110. protected override void Dispose(bool disposing)
  111. {
  112. if (disposing && (components != null))
  113. {
  114. components.Dispose();
  115. }
  116. base.Dispose(disposing);
  117. }
  118.  
  119. #region Windows 窗体设计器生成的代码
  120.  
  121. /// <summary>
  122. /// 设计器支持所需的方法 - 不要
  123. /// 使用代码编辑器修改此方法的内容。
  124. /// </summary>
  125. private void InitializeComponent()
  126. {
  127. this.lblName = new System.Windows.Forms.Label();
  128. this.txtName = new System.Windows.Forms.TextBox();
  129. this.lblAge = new System.Windows.Forms.Label();
  130. this.txtAge = new System.Windows.Forms.TextBox();
  131. this.btnPrevious = new System.Windows.Forms.Button();
  132. this.btnNext = new System.Windows.Forms.Button();
  133. this.btnCurrent = new System.Windows.Forms.Button();
  134. this.chkIsCCP = new System.Windows.Forms.CheckBox();
  135. this.SuspendLayout();
  136. //
  137. // lblName
  138. //
  139. this.lblName.AutoSize = true;
  140. this.lblName.Location = new System.Drawing.Point(, );
  141. this.lblName.Name = "lblName";
  142. this.lblName.Size = new System.Drawing.Size(, );
  143. this.lblName.TabIndex = ;
  144. this.lblName.Text = "姓名:";
  145. //
  146. // txtName
  147. //
  148. this.txtName.Location = new System.Drawing.Point(, );
  149. this.txtName.Name = "txtName";
  150. this.txtName.Size = new System.Drawing.Size(, );
  151. this.txtName.TabIndex = ;
  152. //
  153. // lblAge
  154. //
  155. this.lblAge.AutoSize = true;
  156. this.lblAge.Location = new System.Drawing.Point(, );
  157. this.lblAge.Name = "lblAge";
  158. this.lblAge.Size = new System.Drawing.Size(, );
  159. this.lblAge.TabIndex = ;
  160. this.lblAge.Text = "年龄:";
  161. //
  162. // txtAge
  163. //
  164. this.txtAge.Location = new System.Drawing.Point(, );
  165. this.txtAge.Name = "txtAge";
  166. this.txtAge.Size = new System.Drawing.Size(, );
  167. this.txtAge.TabIndex = ;
  168. //
  169. // btnPrevious
  170. //
  171. this.btnPrevious.Location = new System.Drawing.Point(, );
  172. this.btnPrevious.Name = "btnPrevious";
  173. this.btnPrevious.Size = new System.Drawing.Size(, );
  174. this.btnPrevious.TabIndex = ;
  175. this.btnPrevious.Text = "上一条";
  176. this.btnPrevious.UseVisualStyleBackColor = true;
  177. this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
  178. //
  179. // btnNext
  180. //
  181. this.btnNext.Location = new System.Drawing.Point(, );
  182. this.btnNext.Name = "btnNext";
  183. this.btnNext.Size = new System.Drawing.Size(, );
  184. this.btnNext.TabIndex = ;
  185. this.btnNext.Text = "下一条";
  186. this.btnNext.UseVisualStyleBackColor = true;
  187. this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
  188. //
  189. // btnCurrent
  190. //
  191. this.btnCurrent.Location = new System.Drawing.Point(, );
  192. this.btnCurrent.Name = "btnCurrent";
  193. this.btnCurrent.Size = new System.Drawing.Size(, );
  194. this.btnCurrent.TabIndex = ;
  195. this.btnCurrent.Text = "获取当前编辑对象";
  196. this.btnCurrent.UseVisualStyleBackColor = true;
  197. this.btnCurrent.Click += new System.EventHandler(this.btnCurrent_Click);
  198. //
  199. // chkIsCCP
  200. //
  201. this.chkIsCCP.AutoSize = true;
  202. this.chkIsCCP.Location = new System.Drawing.Point(, );
  203. this.chkIsCCP.Name = "chkIsCCP";
  204. this.chkIsCCP.Size = new System.Drawing.Size(, );
  205. this.chkIsCCP.TabIndex = ;
  206. this.chkIsCCP.Text = "是否党员";
  207. this.chkIsCCP.UseVisualStyleBackColor = true;
  208. //
  209. // Form1
  210. //
  211. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  212. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  213. this.ClientSize = new System.Drawing.Size(, );
  214. this.Controls.Add(this.chkIsCCP);
  215. this.Controls.Add(this.btnCurrent);
  216. this.Controls.Add(this.btnNext);
  217. this.Controls.Add(this.btnPrevious);
  218. this.Controls.Add(this.txtAge);
  219. this.Controls.Add(this.lblAge);
  220. this.Controls.Add(this.txtName);
  221. this.Controls.Add(this.lblName);
  222. this.Name = "Form1";
  223. this.Text = "Form1";
  224. this.ResumeLayout(false);
  225. this.PerformLayout();
  226.  
  227. }
  228.  
  229. #endregion
  230.  
  231. private System.Windows.Forms.Label lblName;
  232. private System.Windows.Forms.TextBox txtName;
  233. private System.Windows.Forms.Label lblAge;
  234. private System.Windows.Forms.TextBox txtAge;
  235. private System.Windows.Forms.Button btnPrevious;
  236. private System.Windows.Forms.Button btnNext;
  237. private System.Windows.Forms.Button btnCurrent;
  238. private System.Windows.Forms.CheckBox chkIsCCP;
  239. #endregion
  240.  
  241. }

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. 023-zabbix性能优化中的几个中肯建议

    随着zabbix的广泛应用,少数人的zabbix服务器在性能上出现瓶颈,或者在未来会出现性能方面的瓶颈,接下来讨论几个有效并且简单的优化方案. 服务器硬件 想通过几个简单的配置让服务器提高成倍的性能, ...

  2. 牛客练习赛26B 烟花 (概率DP)

    链接:https://ac.nowcoder.com/acm/contest/180/B 来源:牛客网 烟花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5 ...

  3. 安装mysql的步骤并利用mysql原始密码修改自定义密码

    1.给刚下载好的mysql软件tar包,进行解包 命令:tar -xf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar 然后利用yum装包 命令:yum -y ins ...

  4. 七、设备驱动中的阻塞与非阻塞 IO(一)

    7.1 阻塞与非阻塞 IO 阻塞操作是指在执行设备操作的时候,若不能获取资源,则挂起进程直到满足可操作的条件后再进行操作.被挂起的进程进入睡眠状态,被从调度器的运行队列移走,直到等待的条件被满足. 非 ...

  5. ztree多种数据包装以及相关设置

    首先来一个完整的ztree代码 html代码 <form id="addTreeDataFrm" method="post" class="fo ...

  6. HDU-2732-leapin'Lizards(最大流, 拆点)

    链接: https://vjudge.net/problem/HDU-2732 题意: Your platoon of wandering lizards has entered a strange ...

  7. SpringBoot框架(5)-- @EableAutoConfiguration项目应用

    场景:在项目中想在当前maven项目中自动装配其他自定义的Maven项目,例如,创建数据库配置中心,被夺多个maven引用,希望简单配置,就实现springboot自动装配数据库配置类. 由此我们联想 ...

  8. python接口自动化三(登录绕开验证码及发帖)

    前言 有些登录的接口会有验证码:短信验证码,图形验证码等,这种登录的话验证码参数可以从后台获取的(或者查数据库最直接). 获取不到也没关系,可以通过添加cookie的方式绕过验证码. 但是这里需要明确 ...

  9. 上采样 及 Sub-pixel Convolution (子像素卷积)

    参考:https://blog.csdn.net/leviopku/article/details/84975282 参考:https://blog.csdn.net/g11d111/article/ ...

  10. 恩友歌 What a friend we've found 歌词

      Verse 1   何等恩友仁慈救主 负我罪孽担我忧 何等权利能将万事 来到耶稣座前求 多少平安我们坐失 多少痛苦冤枉受 都是因为未将万事 来到耶稣座前求     Verse 2   我们有无试探 ...