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

📄 scrollingdatabinging.cs

📁 C#数据库编程。
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace shuJvBanding
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class ScrollingDataBinging : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Button retrieveButton;
		private TextBox textName;
		private TextBox textQuan;
		private TrackBar trackBar;
		private DataSet ds;

		public ScrollingDataBinging()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
			this.ClientSize = new System.Drawing.Size(464,253);
			this.Text = "09_ScrollingDataBinding";
			this.retrieveButton = new Button();
			retrieveButton.Location = new System.Drawing.Point(4,4);
			retrieveButton.Size = new System.Drawing.Size(75,23);
			retrieveButton.TabIndex = 1;
			retrieveButton.Anchor =AnchorStyles.Top| AnchorStyles.Left;
			retrieveButton.Text = "Retrieve";
			retrieveButton.Click += new System.EventHandler(this.retrieveButton_Click);
			this.Controls.Add(this.retrieveButton);

			this.textName = new TextBox();
			textName.Location = new System.Drawing.Point(4,31);
			textName.Text ="Please click retrieve ...";
			textName.TabIndex = 2;
			textName.Anchor = AnchorStyles.Top|AnchorStyles.Left|AnchorStyles.Right ;
			textName.Size = new System.Drawing.Size(456,20);
			textName.Enabled = false;
			this.Controls.Add(this.textName);

			this.textQuan = new TextBox();
			textQuan.Location = new System.Drawing.Point(4,55);
			textQuan.Text ="";
			textQuan.TabIndex = 3;
			textQuan.Anchor = AnchorStyles.Top|AnchorStyles.Left|AnchorStyles.Right ;
			textQuan.Size = new System.Drawing.Size(456,20);
			textQuan.Enabled = false;
			this.Controls.Add(this.textQuan);

			this.trackBar = new TrackBar();
			trackBar.BeginInit();
			trackBar.Dock= DockStyle.Bottom;
			trackBar.Location = new System.Drawing.Point(0,275);
			//textName.Text ="Please click retrieve ...";
			trackBar.TabIndex = 4;
			//trackBar.Anchor = AnchorStyles.Top|AnchorStyles.Left|AnchorStyles.Right ;
			trackBar.Size = new System.Drawing.Size(504,42);
			trackBar.Scroll += new System.EventHandler(this.trackBar_Scroll);
			trackBar.Enabled = false;
			this.Controls.Add(this.trackBar);

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new ScrollingDataBinging());
		}
		protected void retrieveButton_Click(object sender,System.EventArgs e){
			retrieveButton.Enabled =false;
			ds = CreateDataSet();
			textName.DataBindings.Add("Text",ds,"Products.ProductName");
			textQuan.DataBindings.Add("Text",ds,"Products.QuantityPerUnit");
			trackBar.Minimum = 0;
			trackBar.Maximum = this.BindingContext[ds,"products"].Count - 1;
			textName.Enabled = true;
			textQuan.Enabled = true;
			trackBar.Enabled = true;
		}
		protected void trackBar_Scroll(object sender,System.EventArgs e){
			this.BindingContext[ds,"Products"].Position = trackBar.Value;
		}
		private DataSet CreateDataSet(){
			string source = "server = (local);uid = sa;pwd = sa;database = northwind;";
			//server = (local);uid = sa;pwd =sa;database = MyOA;
			string customers = "Select * from Products";
			SqlConnection con = new SqlConnection(source);
			SqlDataAdapter da = new SqlDataAdapter(customers,con);
			DataSet ds = new DataSet();
			da.Fill(ds,"Products");
			return ds;
		}


		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

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