网站首页 > 网络编程教程 > ASP.NET教程 > DataGrid实例(简单易懂,无复杂功能,适合初学者)

DataGrid实例(简单易懂,无复杂功能,适合初学者)

  • 作者:互联网
  • 时间:2009-06-30 16:03:05

使ACCESS数据库,适合初学者,修改连接、查询语句后可直接运行,代码中有注明。

      填充DataSet的步骤
      1、使用数据库连接字符串创建数据库连接对象
      2、用SQL查询语句和数据库连接对象创建数据库适配器dataAdapter
      3、使用DataAdapter的Fill 方法填充DataSet


using System;
using Sy***m.Windows.Forms;
using Sy***m.Data;
using Sy***m.Data.SqlClient;
using Sy***m.Data.OleDb;
//Professional C# 2nd的DATAGRID实例
/**////   


///    This class provides    an example of creating and using a data    grid.
///   

public class DisplayTabularData : Sy***m.Windows.Forms.Form
{
    private Sy***m.Windows.Forms.Button retrieveButton;
    private Sy***m.Windows.Forms.DataGrid dataGrid;

    /**////   


    ///    Construct the window.
    ///   

    ///   
    ///    This method    constructs the window by creating both the data    grid and the button.
    ///   

    public DisplayTabularData()
    {
        th***AutoScaleBaseSize = new Sy***m.Drawing.Size(5, 13);
        th***ClientSize = new Sy***m.Drawing.Size(464, 253);
        th***Text = "01_DisplayTabularData";
        th***dataGrid = new Sy***m.Windows.Forms.DataGrid();
        da***rid.BeginInit();
        da***rid.Location = new Sy***m.Drawing.Point(8, 8);
        da***rid.Size = new Sy***m.Drawing.Size(448, 208);
        da***rid.TabIndex = 0;
        da***rid.Anchor = An***rStyles.Bottom | An***rStyles.Top | An***rStyles.Left | An***rStyles.Right;
        th***Controls.Add(th***dataGrid);
        da***rid.EndInit();
        th***retrieveButton = new Sy***m.Windows.Forms.Button();
        re***eveButton.Location = new Sy***m.Drawing.Point(384, 224);
        re***eveButton.Size = new Sy***m.Drawing.Size(75, 23);
        re***eveButton.TabIndex = 1;
        re***eveButton.Anchor = An***rStyles.Bottom | An***rStyles.Right;
        re***eveButton.Text = "Retrieve";
        re***eveButton.Click += new Sy***m.EventHandler(th***retrieveButton_Click);
        th***Controls.Add(th***retrieveButton);
    }

    /**////   


    ///    Retrieve the data
    ///   

    ///   
    ///   
    protected void retrieveButton_Click(object sender, Sy***m.EventArgs e)
    {
        re***eveButton.Enabled = false;

        string source = @"Pr***der=Microsoft.Jet.OLEDB.4.0;Data Source=C:Documents and SettingsManioMy DocumentsPrintManagerprogramPrintManageV1DataBasePr***DB.mdb";
        string select = "SELECT * FROM MainInfo";

        /**/////////////////////////////////
        //填充DataSet的步骤
        //1、使用数据库连接字符串创建数据库连接对象
        //    2、用SQL查询语句和数据库连接对象创建数据库适配器dataAdapter
        //        3、使用DataAdapter的Fill 方法填充DataSet

        OleDbConnection OleCon = new OleDbConnection(source);

        OleDbDataAdapter da = new OleDbDataAdapter(select,OleCon);

        DataSet ds = new DataSet();

        da.Fill(ds, "MainInfo");

        da***rid.SetDataBinding(ds, "MainInfo");    //DataGrid的数据绑定,使用DataSet 和 数据库的表名
    }

    /**////   


    ///    Display    the    application    window
    ///   

    static void Main()
    {
        Ap***cation.Run(new DisplayTabularData());
    }
}