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

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

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

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

None.gif public   class  DisplayTabularData : System.Windows.Forms.Form
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private System.Windows.Forms.Button retrieveButton;
InBlock.gif    
private System.Windows.Forms.DataGrid dataGrid;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////    <summary>
InBlock.gif    
///    Construct the window.
InBlock.gif    
///    </summary>
InBlock.gif    
///    <remarks>
InBlock.gif    
///    This method    constructs the window by creating both the data    grid and the button.
ExpandedSubBlockEnd.gif    
///    </remarks>

InBlock.gif    public DisplayTabularData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.AutoScaleBaseSize = new System.Drawing.Size(513);
InBlock.gif        
this.ClientSize = new System.Drawing.Size(464253);
InBlock.gif        
this.Text = "01_DisplayTabularData";
InBlock.gif        
this.dataGrid = new System.Windows.Forms.DataGrid();
InBlock.gif        dataGrid.BeginInit();
InBlock.gif        dataGrid.Location 
= new System.Drawing.Point(88);
InBlock.gif        dataGrid.Size 
= new System.Drawing.Size(448208);
InBlock.gif        dataGrid.TabIndex 
= 0;
InBlock.gif        dataGrid.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
InBlock.gif        
this.Controls.Add(this.dataGrid);
InBlock.gif        dataGrid.EndInit();
InBlock.gif        
this.retrieveButton = new System.Windows.Forms.Button();
InBlock.gif        retrieveButton.Location 
= new System.Drawing.Point(384224);
InBlock.gif        retrieveButton.Size 
= new System.Drawing.Size(7523);
InBlock.gif        retrieveButton.TabIndex 
= 1;
InBlock.gif        retrieveButton.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Right;
InBlock.gif        retrieveButton.Text 
= "Retrieve";
InBlock.gif        retrieveButton.Click 
+= new System.EventHandler(this.retrieveButton_Click);
InBlock.gif        
this.Controls.Add(this.retrieveButton);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////    <summary>
InBlock.gif    
///    Retrieve the data
InBlock.gif    
///    </summary>
InBlock.gif    
///    <param name="sender"> </param>
ExpandedSubBlockEnd.gif    
///    <param name="e"> </param>

InBlock.gif    protected void retrieveButton_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        retrieveButton.Enabled 
= false;
InBlock.gif
InBlock.gif        
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Manio\My Documents\PrintManager\program\PrintManageV1\DataBase\PrintDB.mdb";
InBlock.gif        
string select = "SELECT * FROM MainInfo";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/////
InBlock.gif        //填充DataSet的步骤
InBlock.gif        
//1、使用数据库连接字符串创建数据库连接对象
InBlock.gif        
//    2、用SQL查询语句和数据库连接对象创建数据库适配器dataAdapter
InBlock.gif        
//        3、使用DataAdapter的Fill 方法填充DataSet
InBlock.gif

InBlock.gif        OleDbConnection OleCon 
= new OleDbConnection(source);
InBlock.gif
InBlock.gif        OleDbDataAdapter da 
= new OleDbDataAdapter(select,OleCon);
InBlock.gif
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif
InBlock.gif        da.Fill(ds, 
"MainInfo");
InBlock.gif
InBlock.gif        dataGrid.SetDataBinding(ds, 
"MainInfo");    //DataGrid的数据绑定,使用DataSet 和 数据库的表名
ExpandedSubBlockEnd.gif
    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////    <summary>
InBlock.gif    
///    Display    the    application    window
ExpandedSubBlockEnd.gif    
///    </summary>

InBlock.gif    static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Application.Run(
new DisplayTabularData());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
posted on 2006-03-31 21:47 马牛 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/manio/archive/2006/03/31/363915.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值