简单数据绑定
在新建的工程中添加㙬个文本框。
首先要建立与数据源的连接,用结果集填充创建的DataSet。一旦提取了所有的数据,就进行绑定:
textBox1.DataBindings.Add("Text",ds,"Employees.FirstName");
由于TextBox控件一次只能显示一个值,所以,使用Add方法将其Text属性绑定到Employees表的FirstName列。
Add方法采用下面的参数:
要绑定到的属性的名称
数据源
字符串文字,数据成员,用来描述被绑定值的准确位置
这里使用DataSet作数据源,数据成员只能是Table.Column形式的路径。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Banding
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string str = @"server=localhost;Integrated Security=SSPI;database=Northwind";

string SQL = "SELECT * FROM Employees";
SqlConnection conn = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter(SQL, conn);
DataSet ds = new DataSet();

da.Fill(ds, "Employees");
textBox1.DataBindings.Add("Text", ds, "Employees.FirstName");
textBox2.DataBindings.Add("Text",ds,"Employees.LastName");

}
}
}
复杂数据绑定
1.绑定到数据表:
新建一个工程,在窗体中添加一个列表框,两个文本框控件。
通过设置DataScoure和DisplayMember属性绑定ListBox控件。
DataScource属性采用集合对象作为数据源,如DataTable或DataSet。
DisplayMember属性采用数据源内的数据成员,用数据填充ListBox。
这里使用DataTable作为数据源,所以DisplayMember属性采用列名绑定到控件。
类似地,通过给DataBindings集合添加绑定以及将DataTable中的列名作为数据成员,把文本绑定到DataTable上:
textBox.DataBingdings.Add("Text",myTable,"FirstName");
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Banding
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string conStr = @"server=localhost;integrated security=SSPI;database=Northwind";

//从Employees表中获取数据
string SQL = "SELECT * FROM Employees";
SqlConnection Conn = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(SQL, Conn);
//填充数据
da.Fill(dataSet1, "Employees");

DataTable myTable = dataSet1.Tables["Employees"];
//把列表框绑定到对象上
listBox1.DataSource = myTable;
listBox1.DisplayMember = "FirstName";
//把文本框绑定到对象上
textBox1.DataBindings.Add("Text", myTable, "Title");
textBox2.DataBindings.Add("Text", myTable, "Address");

}

private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
}

private void btnBack_Click(object sender, EventArgs e)
{
}

private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
{
}
}
}
2.绑定到数据集
将DataSet作为数据源,这个类实现IListSource接口。
private void Form1_Load(object sender, EventArgs e)
string conStr = @"server=localhost;integrated security=SSPI;database=Northwind";

//从Employees表中获取数据
string SQL = "SELECT * FROM Employees";
SqlConnection Conn = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(SQL, Conn);
//绑定到数据集
da.Fill(dataSet1, "Employees");
label1.DataBindings.Add("Text", dataSet1, "Employees.Title");
label2.DataBindings.Add("Text", dataSet1, "Employees.Address");

}
3.绑定到数据视图
private void Form1_Load(object sender, EventArgs e)
{
string conStr = @"server=localhost;integrated security=SSPI;database=Northwind";

//从Employees表中获取数据
string SQL = "SELECT * FROM Employees";
SqlConnection Conn = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(SQL, Conn);
//绑定到数据集
da.Fill(dataSet1, "Employees");
DataTable tb=dataSet1.Tables["Employees"];
//创建数据视图,设置过滤器,及排序
DataView dv = new DataView(tb, "Country='UK'", "FirstName", DataViewRowState.CurrentRows);
//绑定到文本框
label1.DataBindings.Add("Text", dv, "Title");
label2.DataBindings.Add("Text", dv, "Address");
}
在新建的工程中添加㙬个文本框。
首先要建立与数据源的连接,用结果集填充创建的DataSet。一旦提取了所有的数据,就进行绑定:
textBox1.DataBindings.Add("Text",ds,"Employees.FirstName");
由于TextBox控件一次只能显示一个值,所以,使用Add方法将其Text属性绑定到Employees表的FirstName列。
Add方法采用下面的参数:
要绑定到的属性的名称
数据源
字符串文字,数据成员,用来描述被绑定值的准确位置
这里使用DataSet作数据源,数据成员只能是Table.Column形式的路径。



































复杂数据绑定
1.绑定到数据表:
新建一个工程,在窗体中添加一个列表框,两个文本框控件。
通过设置DataScoure和DisplayMember属性绑定ListBox控件。
DataScource属性采用集合对象作为数据源,如DataTable或DataSet。
DisplayMember属性采用数据源内的数据成员,用数据填充ListBox。
这里使用DataTable作为数据源,所以DisplayMember属性采用列名绑定到控件。
类似地,通过给DataBindings集合添加绑定以及将DataTable中的列名作为数据成员,把文本绑定到DataTable上:
textBox.DataBingdings.Add("Text",myTable,"FirstName");

































































2.绑定到数据集
将DataSet作为数据源,这个类实现IListSource接口。
















3.绑定到数据视图



















