The Flower of Evil
asp.net & XML Web Service

导航

 
代码:
  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7using System.Data.SqlClient;
  8
  9namespace 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

程序运行截图:
DataTable.gif

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

posted on 2005-11-08 14:12  The Flower of Evil  阅读(515)  评论(0)    收藏  举报