using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace useDataGrid
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(64, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(472, 160);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(648, 269);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "使用DataGrid控件来显示数据";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\db1.mdb";
OleDbConnection myOleDbConnection= new OleDbConnection(connString);
string selectString = "SELECT * FORM 表1";
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand ();
myOleDbCommand.CommandText = selectString;
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter ();
myDataAdapter.SelectCommand = myOleDbCommand;
DataSet ds = new DataSet();
myOleDbConnection.Open ();
string DataTableName ="表1";
//ds.Clear ();
myDataAdapter.Fill (ds,DataTableName);
DataTable myDataTable = ds.Tables [DataTableName];
dataGrid1.DataSource = ds.DefaultViewManager;
}
}
}
这一句 出现异常 myDataAdapter.Fill (ds,DataTableName);
高手指教一下
[此贴子已经被作者于2007-3-28 22:42:37编辑过]