首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 开源 FAQ 第二书店 博文视点 程序员
频道: 研发 数据库 中间件 信息化 视频 .NET Java 游戏 移动 服务: 人才 外包 培训
    图书品种:235680
       
热门搜索: ASP.NET Ajax Spring Hibernate Java

22.1.1  显示列表数据

21章介绍了选择数据和把数据放在一个数据表中的各种方式,但仅使用了Console.WriteLine()方法,以非常基本的形式显示数据。

第一个示例将说明如何获取一些数据,并在DataGrid控件中显示,为此,建立一个新的应用程序DisplayTabularData,如图22-1所示。

  22-1

这个简单的应用程序从Northwind数据库的customer表中选择每个记录,在DataGrid中把它们显示给用户。其代码如下所示:

using System;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

 

public class DisplayTabularData : System.Windows.Forms.Form

{

   private System.Windows.Forms.Button retrieveButton;

   private System.Windows.Forms.DataGrid dataGrid;

   public DisplayTabularData()

   {

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

      this.ClientSize = new System.Drawing.Size(464, 253);

      this.Text = "01_DisplayTabularData";

代码的起始部分创建主窗口类,为该类定义实例变量,设置窗口的一些属性。接着创建DataGrid网格控件。

      this.dataGrid = new System.Windows.Forms.DataGrid();

      dataGrid.BeginInit();

      dataGrid.Location = new System.Drawing.Point(8, 8);

      dataGrid.Size = new System.Drawing.Size(448, 208);

      dataGrid.TabIndex = 0;

      dataGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |

                        AnchorStyles.Left | AnchorStyles.Right;

      this.Controls.Add(this.dataGrid);

      dataGrid.EndInit();

第二行dataGrid.BeginInit();禁止激发该网格上的事件,在对控件进行大量的修改时,这是很必要的。如果没有禁止该事件,对网格的每次修改都会刷新屏幕。然后设置控件的位置和大小,定义标签索引,把控件固定在窗口的左上角和右下角,便于跟踪主应用程序窗口的对应控件。

下面创建按钮。在初始化按钮时,基本步骤是一样的:

      this.retrieveButton = new System.Windows.Forms.Button();

      retrieveButton.Location = new System.Drawing.Point(384, 224);

      retrieveButton.Size = new System.Drawing.Size(75, 23);

      retrieveButton.TabIndex = 1;

      retrieveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

      retrieveButton.Text = "Retrieve";

      retrieveButton.Click += new System.EventHandler

                                  (this.retrieveButton_Click);

      this.Controls.Add(this.retrieveButton);

   }

按钮会引发Click事件,所以定义该事件的事件处理程序retrieveButton_Click

   protected void retrieveButton_Click(object sender, System.EventArgs e)

   {

      retrieveButton.Enabled = false;

      string source = "server=(local)\\NetSDK;" +

                   "uid=QSUser;pwd=QSPassword;" +

                   "database=Northwind";

在选择了Customers表中的数据填充DataSet后,调用SetDataBindingDataSet绑定到网格上。这个方法的参数是DataSet和其中要显示的表名。该网格一次只显示一个DataTable中的数据,即使DataSet包含多个表,也是这样。本章将举一个示例,显示带有多个DataTableDataSet中的数据。当然,DataSet中的数据可以来自于许多实际的数据库表(或者多个表的视图)

      string select = "SELECT * FROM Customers" ;

      SqlConnection conn = new SqlConnection(source);

      SqlDataAdapter da = new SqlDataAdapter( select , conn);

      DataSet ds = new DataSet();

      da.Fill(ds , "Customers");

      dataGrid.SetDataBinding(ds , "Customers");

   }

   static void Main()

   {

      Application.Run(new DisplayTabularData());

   }

}

要编译该示例,在命令提示符上输入下述命令:

csc /t:winexe /debug+ /r:System.dll /r:System.Data.dll /r:system.windows.forms.dll

    /recurse:*.cs

/recurse:*.cs参数会编译当前目录及其子目录下的所有.cs文件,这里使用该参数是为了确保把所有相关的代码文件都包含到可执行文件中。

查看所有评论(0)条】

最近评论



正在载入评论列表...
热点评论