⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 CSHARP数据集绑定操作,实现绑定功能操作.
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataBindingExampleCSharp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.Label Label2;
		private System.Windows.Forms.TextBox txt1;
		private System.Windows.Forms.TextBox txt2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txt1 = new System.Windows.Forms.TextBox();
			this.txt2 = new System.Windows.Forms.TextBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// txt1
			// 
			this.txt1.Location = new System.Drawing.Point(56, 48);
			this.txt1.Name = "txt1";
			this.txt1.Size = new System.Drawing.Size(184, 20);
			this.txt1.TabIndex = 0;
			this.txt1.Text = "";
			// 
			// txt2
			// 
			this.txt2.Location = new System.Drawing.Point(56, 104);
			this.txt2.Name = "txt2";
			this.txt2.Size = new System.Drawing.Size(184, 20);
			this.txt2.TabIndex = 1;
			this.txt2.Text = "";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(56, 32);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(176, 16);
			this.Label1.TabIndex = 3;
			this.Label1.Text = "Data is formatted before display";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(56, 88);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(176, 16);
			this.Label2.TabIndex = 4;
			this.Label2.Text = "Default binding behaviour";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(304, 165);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Label2,
																		  this.Label1,
																		  this.txt2,
																		  this.txt1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.Text = "Controlling Data Binding";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		
		private MyDateTime oDt = new MyDateTime();
		
		//The binding object. We will use the format and parse events generated by this object.
		private Binding oBinding;

		private void Form1_Load(object sender, System.EventArgs e)
		{
			
			//A binding object declared above is explicitly created. This object 
			//binds the text field to the "GetDateTime" property of the MyDateTime class.  
		    //The binding object is then added to the textbox's databindings collection.
			oBinding = new Binding("Text", oDt, "GetDateTime");
			
			//The event handler is added
			oBinding.Format += new System.Windows.Forms.ConvertEventHandler(this.oBinding_Format);
			
			txt1.DataBindings.Add(oBinding);

			//Here, the binding object is not explicitly created and hence
			//no events will triggered and the datetime is displayed in  "MM/dd/yyyy hh:mm:ss"
			//format only.
			txt2.DataBindings.Add("Text", oDt, "GetDateTime");
		}
		

		//This event is triggered right before the txt1's text field displays the datetime value that it
		//obtains from oDt object (the datasource). Here, we have changed the format of display to "MM/dd/yy" 
		//although the actual data pulled from oDt is in "MM/dd/yyyy hh:mm:ss" format.
		private void oBinding_Format(object sender, System.Windows.Forms.ConvertEventArgs e) 
		{
			e.Value = ((DateTime)e.Value).ToString("MM/dd/yy");
		}

	}

	//This class exposes one property "GetDateTime" that returns a datetime 
	//value in the format "MM/dd/yyyy hh:mm:ss". This behaviour is similar 
	//to getting the datetime value from a sql server database column of 
	//the type datetime.
	public class MyDateTime 
	{
		private DateTime dt;

		public MyDateTime() 
		{
			this.dt = DateTime.Now;
		}
		
		public DateTime GetDateTime
		{
			get
			{
				return dt;
			}
			set
			{
				dt = value;
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -