DataGrid實例(簡單易懂,無複雜功能,適合初學者)

使ACCESS數據庫,適合初學者,修改連接、查詢語句後可直接運行,代碼中有註明。

      填充DataSet的步驟
      1、使用數據庫連接字符串創建數據庫連接對象
      2、用SQL查詢語句和數據庫連接對象創建數據庫適配器dataAdapter
      3、使用DataAdapter的Fill 方法填充DataSet

 
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
//Professional C# 2nd的DATAGRID實例
/**////    <summary>
///    This class provides    an example of creating and using a data    grid.
///    </summary>

public class DisplayTabularData : System.Windows.Forms.Form
{
    
private System.Windows.Forms.Button retrieveButton;
    
private System.Windows.Forms.DataGrid dataGrid;

    
/**////    <summary>
    
///    Construct the window.
    
///    </summary>
    
///    <remarks>
    
///    This method    constructs the window by creating both the data    grid and the button.
    
///    </remarks>

    public DisplayTabularData()
    
{
        
this.AutoScaleBaseSize = new System.Drawing.Size(513);
        
this.ClientSize = new System.Drawing.Size(464253);
        
this.Text = "01_DisplayTabularData";
        
this.dataGrid = new System.Windows.Forms.DataGrid();
        dataGrid.BeginInit();
        dataGrid.Location 
= new System.Drawing.Point(88);
        dataGrid.Size 
= new System.Drawing.Size(448208);
        dataGrid.TabIndex 
= 0;
        dataGrid.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        
this.Controls.Add(this.dataGrid);
        dataGrid.EndInit();
        
this.retrieveButton = new System.Windows.Forms.Button();
        retrieveButton.Location 
= new System.Drawing.Point(384224);
        retrieveButton.Size 
= new System.Drawing.Size(7523);
        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);
    }


    
/**////    <summary>
    
///    Retrieve the data
    
///    </summary>
    
///    <param name="sender"> </param>
    
///    <param name="e"> </param>

    protected void retrieveButton_Click(object sender, System.EventArgs e)
    
{
        retrieveButton.Enabled 
= false;

        
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Documents and SettingsManioMy DocumentsPrintManagerprogramPrintManageV1DataBasePrintDB.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");

        dataGrid.SetDataBinding(ds, 
"MainInfo");    //DataGrid的數據綁定,使用DataSet 和 數據庫的表名
    }


    
/**////    <summary>
    
///    Display    the    application    window
    
///    </summary>

    static void Main()
    
{
        Application.Run(
new DisplayTabularData());
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章