DataGrid实例(简单易懂,无复杂功能,适合初学者)
点击次数:21 次 发布日期:2008-11-26 14:23:23 作者:源代码网
|
源代码网推荐 源代码网推荐 填充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(5, 13); 源代码网推荐 this.ClientSize = new System.Drawing.Size(464, 253); 源代码网推荐 this.Text = "01_DisplayTabularData"; 源代码网推荐 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(); 源代码网推荐 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); 源代码网推荐 } 源代码网推荐 源代码网推荐 /**//// <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()); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
