DataGrid实例(简单易懂,无复杂功能,适合初学者)
2006-09-09 17:53
776 查看
使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(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());
}
}
填充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());
}
}
相关文章推荐
- DataGrid实例(简单易懂,无复杂功能,适合初学者)
- DataGrid实例(简单易懂,无复杂功能,适合初学者)
- DataGrid实例(简单易懂,无复杂功能,适合初学者)
- Android SharedPreferences应用 实现本地注册登陆 功能简单易懂(实例)
- Jquery实现select multiple左右添加和删除功能的简单实例
- Linux socket 编程实例——简单易懂,太好了!
- Android 访问C#.Net Webservice (简单例子适合初学者)
- XNA Game Studio是一套有着强大功能和简单界面的游戏制作平台,游戏开发商和游戏玩家都可以使用这套工具开发针对Windows XP以及Xbox360的游戏,XNA Game Studio分为两种版本,一种是面向初学者的EXPRESS版本,还有一种
- XNA Game Studio是一套有着强大功能和简单界面的游戏制作平台,游戏开发商和游戏玩家都可以使用这套工具开发针对Windows XP以及Xbox360的游戏,XNA Game Studio分为两种版本,一种是面向初学者的EXPRESS版本,还有一种是面向专业用户的专业版。用户使用EXPRESS版本开发游戏完全免费,并且可以随意在PC上发行,不过用该工具开发的游戏若是在360上网络发行,就需要交纳99美元的年费。
- 在html中div+css布局的简单应用(适合初学者了解)
- js实现简单div拖拽功能实例
- JEECG 简单实例讲解自定义表单功能
- php和数据库结合的一个简单的web实例 代码分析 (php初学者)
- 方便实用的jQuery checkbox复选框全选功能简单实例
- 开发,配置,调用-三步走构建简单的asp.net webservice(C#)-适合初学者阅读
- springMVC3.2+Hibernate4+freemarker入门简单小例子,适合初学者
- php和数据库结合的一个 简单的web实例 代码分析 (php初学者)
- JMeter简单性能测试(适合初学者)
- Flashbuilder4.5 + eclipse for j2ee + Blazeds 简单实例(适合新手)