Csharp/C Sharp/Database ADO.net/Data Bind TextBox
Содержание
Bind Database table column to TextBox
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.ruponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
private void InitializeComponent(){
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox2.Location = new System.Drawing.Point(8, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(184, 20);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 76);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql = @"select * from employee ";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employee");
// Bind to FirstName column of the Employees table
textBox1.DataBindings.Add("text", ds, "employee.firstname");
// Bind to LastName column of the Employees table
textBox2.DataBindings.Add("text", ds, "employee.lastname");
}
}
Bind DataSet to TextBox
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
public class Form1 : Form {
public Form1() {
InitializeComponent();
DataSet ds = CreateDataSet();
textBox1.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");
}
private DataSet CreateDataSet() {
string customers = "SELECT * FROM Products";
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
SqlDataAdapter da = new SqlDataAdapter(customers, con);
da.Fill(ds, "Products");
}
return ds;
}
private void InitializeComponent() {
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(370, 20);
this.textBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(394, 44);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "SimpleDataBinding";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox textBox1;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Bind String array to a TextBox
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox textBox1;
private System.ruponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
private void InitializeComponent(){
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(16, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(144, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(176, 36);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
String[] names = new String[] {"A", "B", "C", "D", "E"};
textBox1.DataBindings.Add("text", names, null);
}
}
DataView Binding to TextBox
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Data.DataSet dataSet1;
private System.ruponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.dataSet1 = new System.Data.DataSet();
((System.ruponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(16, 0);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox2.Location = new System.Drawing.Point(16, 24);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("en-GB");
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(128, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox2,this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ruponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
this.ResumeLayout(false);
}
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string SQL = @"select * from employee";
SqlConnection Conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter(SQL, Conn);
da.Fill(dataSet1, "Employee");
DataTable myTable = dataSet1.Tables["Employee"];
DataView dv = new DataView(myTable, "FirstName="Z"","FirstName", DataViewRowState.CurrentRows);
textBox1.DataBindings.Add("Text", dv, "FirstName");
textBox2.DataBindings.Add("Text", dv, "LastName");
}
}
Simple DataBinding to TextBox
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form {
public Form1() {
InitializeComponent();
DataSet ds = CreateDataSet();
textBox.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");
}
private DataSet CreateDataSet() {
string customers = "SELECT * FROM Products";
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["northwind"].ConnectionString)) {
SqlDataAdapter da = new SqlDataAdapter(customers, con);
da.Fill(ds, "Products");
}
return ds;
}
private void InitializeComponent() {
this.textBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(13, 13);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(477, 20);
this.textBox.TabIndex = 0;
//
// Form1
this.Controls.Add(this.textBox);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox textBox;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.EnableRTLMirroring();
Application.Run(new Form1());
}
}