1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
using System.Data.SqlClient;
8
9
namespace Arrays
10
{
11
/// <summary>
12
/// Form1 的摘要说明。
13
/// </summary>
14
public class Form1 : System.Windows.Forms.Form
15
{
16
private System.Windows.Forms.ListBox listBox1;
17
private System.Windows.Forms.TextBox textBox1;
18
private System.Windows.Forms.TextBox textBox2;
19
private System.Data.DataSet dataSet1;
20
/// <summary>
21
/// 必需的设计器变量。
22
/// </summary>
23
private System.ComponentModel.Container components = null;
24
25
public Form1()
26
{
27
//
28
// Windows 窗体设计器支持所必需的
29
//
30
InitializeComponent();
31
32
//
33
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
34
//
35
}
36
37
/// <summary>
38
/// 清理所有正在使用的资源。
39
/// </summary>
40
protected override void Dispose( bool disposing )
41
{
42
if( disposing )
43
{
44
if (components != null)
45
{
46
components.Dispose();
47
}
48
}
49
base.Dispose( disposing );
50
}
51
52
Windows 窗体设计器生成的代码
108
109
/// <summary>
110
/// 应用程序的主入口点。
111
/// </summary>
112
[STAThread]
113
static void Main()
114
{
115
Application.Run(new Form1());
116
}
117
118
private void Form1_Load(object sender, System.EventArgs e)
119
{
120
string ConStr=@"Data Source=localhost; Initial Catalog=Northwind;Integrated Security=SSPI";
121
string SQL="SELECT * FROM Employees";
122
SqlConnection Conn = new SqlConnection(ConStr);
123
SqlDataAdapter da = new SqlDataAdapter(SQL,Conn);
124
da.Fill(dataSet1,"Employees");
125
DataTable myTable = dataSet1.Tables["Employees"];
126
listBox1.DataSource=myTable;
127
listBox1.DisplayMember="FirstName";
128
textBox1.DataBindings.Add("Text",myTable,"FirstName");
129
textBox2.DataBindings.Add("Text",myTable,"LastName");
130
}
131
}
132
}
133
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7
using System.Data.SqlClient;8

9
namespace Arrays10
{11
/// <summary>12
/// Form1 的摘要说明。13
/// </summary>14
public class Form1 : System.Windows.Forms.Form15
{16
private System.Windows.Forms.ListBox listBox1;17
private System.Windows.Forms.TextBox textBox1;18
private System.Windows.Forms.TextBox textBox2;19
private System.Data.DataSet dataSet1;20
/// <summary>21
/// 必需的设计器变量。22
/// </summary>23
private System.ComponentModel.Container components = null;24

25
public Form1()26
{27
//28
// Windows 窗体设计器支持所必需的29
//30
InitializeComponent();31

32
//33
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码34
//35
}36

37
/// <summary>38
/// 清理所有正在使用的资源。39
/// </summary>40
protected override void Dispose( bool disposing )41
{42
if( disposing )43
{44
if (components != null) 45
{46
components.Dispose();47
}48
}49
base.Dispose( disposing );50
}51

52
Windows 窗体设计器生成的代码108

109
/// <summary>110
/// 应用程序的主入口点。111
/// </summary>112
[STAThread]113
static void Main() 114
{115
Application.Run(new Form1());116
}117

118
private void Form1_Load(object sender, System.EventArgs e)119
{120
string ConStr=@"Data Source=localhost; Initial Catalog=Northwind;Integrated Security=SSPI";121
string SQL="SELECT * FROM Employees";122
SqlConnection Conn = new SqlConnection(ConStr);123
SqlDataAdapter da = new SqlDataAdapter(SQL,Conn);124
da.Fill(dataSet1,"Employees");125
DataTable myTable = dataSet1.Tables["Employees"];126
listBox1.DataSource=myTable;127
listBox1.DisplayMember="FirstName";128
textBox1.DataBindings.Add("Text",myTable,"FirstName");129
textBox2.DataBindings.Add("Text",myTable,"LastName");130
}131
}132
}133

程序运行截图:
工作原理
这个例子把ListBox控件绑定到了从填充过的DataSet提取的DataTable对象上,如下面的代码所示:
DataTable myTable = dataSet1.Tables["Employees"];
设计时通过设置DataSource和DisplayMember属性绑定ListBox控件。DataSource属性采用集合对象作为数据
源,如DataTable或DataSet。DisplayMember属性采用数据源内部的成员,用数据填充ListBox。这个例子中,
由于使用DataTable对象作为数据源,所以DisplayMember属性采用列名称绑定到控件。


浙公网安备 33010602011771号