关于利用DataGrid显示数据的方法; 可以考虑将DataGrid的AutoGenerateColumn的属性设置为false,try it! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* 数据库的连接与修改 OleDbConnection mysword 2004.10.30*/using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.OleDb;namespace CustomerEditor{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu menuMain; private System.Windows.Forms.MenuItem menuData; private System.Windows.Forms.MenuItem menuDataConnect; private System.Windows.Forms.OpenFileDialog dialogOpenFile; private OleDbConnection _connection; private System.Windows.Forms.MenuItem menuDataLoad; private System.Windows.Forms.DataGrid dataGridCustomers; private System.Windows.Forms.MenuItem menuDataSaveChanges; private System.Windows.Forms.MenuItem menuExit; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { Disconnect(); if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.menuMain = new System.Windows.Forms.MainMenu(); this.menuData = new System.Windows.Forms.MenuItem(); this.menuDataConnect = new System.Windows.Forms.MenuItem(); this.dialogOpenFile = new System.Windows.Forms.OpenFileDialog(); this.dataGridCustomers = new System.Windows.Forms.DataGrid(); this.menuDataLoad = new System.Windows.Forms.MenuItem(); this.menuDataSaveChanges = new System.Windows.Forms.MenuItem(); this.menuExit = new System.Windows.Forms.MenuItem(); ((System.ComponentModel.ISupportInitialize)(this.dataGridCustomers)).BeginInit(); this.SuspendLayout(); // // menuMain // this.menuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuData}); // // menuData // this.menuData.Index = 0; this.menuData.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuDataConnect, this.menuDataLoad, this.menuDataSaveChanges, this.menuExit}); this.menuData.Text = "数据库"; // // menuDataConnect // this.menuDataConnect.Index = 0; this.menuDataConnect.Text = "连接数据库"; this.menuDataConnect.Click += new System.EventHandler(this.menuDataConnect_Click); // // dataGridCustomers // this.dataGridCustomers.DataMember = ""; this.dataGridCustomers.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridCustomers.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridCustomers.Location = new System.Drawing.Point(0, 0); this.dataGridCustomers.Name = "dataGridCustomers"; this.dataGridCustomers.Size = new System.Drawing.Size(792, 553); this.dataGridCustomers.TabIndex = 0; // // menuDataLoad // this.menuDataLoad.Index = 1; this.menuDataLoad.Text = "加载数据表"; this.menuDataLoad.Click += new System.EventHandler(this.menuDataLoad_Click); // // menuDataSaveChanges // this.menuDataSaveChanges.Index = 2; this.menuDataSaveChanges.Text = "保存修改"; this.menuDataSaveChanges.Click += new System.EventHandler(this.menuDataSaveChanges_Click); // // menuExit // this.menuExit.Index = 3; this.menuExit.Text = "退出"; this.menuExit.Click += new System.EventHandler(this.menuExit_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(792, 553); this.Controls.Add(this.dataGridCustomers); this.Menu = this.menuMain; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "数据库操作(OleDbConnection)"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; ((System.ComponentModel.ISupportInitialize)(this.dataGridCustomers)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void menuDataConnect_Click(object sender, System.EventArgs e) { Connect(); } public void Connect() { if(dialogOpenFile.ShowDialog(this)==DialogResult.OK) { try { string connectionString=string.Format( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;", dialogOpenFile.FileName); OleDbConnection newConnection= new OleDbConnection(connectionString); newConnection.Open(); Connection=newConnection; } catch(Exception ex) { HandleException("a connection could not be made.",ex); } } } public void HandleException(string message,Exception ex) { MessageBox.Show(this,string.Format("{0}\n{1}:{2}", message,ex.GetType().ToString(),ex.Message)); } public OleDbConnection Connection { get { return _connection; } set { Disconnect(); _connection=value; } } public void Disconnect() { if(_connection!=null) { if(_connection.State!=ConnectionState.Closed) _connection.Close(); _connection=null; } } private void menuDataLoad_Click(object sender, System.EventArgs e) { LoadData(); } public void LoadData() { if(_connection==null) { MessageBox.Show(this,"you must connect to a database."); return; } OleDbCommand command=null; OleDbDataAdapter adapter=null; try { command=_connection.CreateCommand(); command.CommandText="newpost";//表的指定 command.CommandType=CommandType.TableDirect; adapter=new OleDbDataAdapter(command); DataSet dataset=new DataSet(); adapter.Fill(dataset); dataGridCustomers.DataSource=dataset; } catch(Exception ex) { HandleException("the data could not be loaded.",ex); } finally { if(adapter!=null) adapter.Dispose(); if(command!=null) command.Dispose(); } } private void menuExit_Click(object sender, System.EventArgs e) { this.Close(); } private void menuDataSaveChanges_Click(object sender, System.EventArgs e) { SaveChanges(); } public void SaveChanges() { if(_connection==null) { MessageBox.Show("you must connect to a database."); return; } DataSet dataset=(DataSet)dataGridCustomers.DataSource; if(dataset==null) { MessageBox.Show("you must load a dataset."); return; } OleDbCommand command=null; OleDbDataAdapter adapter=null; try { command=_connection.CreateCommand(); command.CommandText="newpost";//表的指定 command.CommandType=CommandType.TableDirect; adapter=new OleDbDataAdapter(command); OleDbCommandBuilder builder=new OleDbCommandBuilder(adapter); adapter.Update(dataset); MessageBox.Show("changes have been saved."); } finally { if(adapter!=null) adapter.Dispose(); if(command!=null) command.Dispose(); } } }}=================================== C# 结构体和数组转化问题 C# winform如何设计一个公共类 如何转化Enum为string[] 【星星三角激辩问题!请进来一起甄别秋毫】C#中的对象(类的实例)和引用变量(引用实例的那个变量)问题讨论 如何让datagridview里的checkbox列默认选中状态 100分求助.实在是郁闷 一般方法能行,但用存储过程为什么不能绑定呢????急呀 在线急求大神解决 各位前辈请赐教!!!!!!(这是一个值得思考的问题) 来抢分!!那位知道MS.net正式版和beta2有什么改进?在哪里有介绍的文章? 菜鸟问题!请高手帮助啊! 关于TEXTBOX
数据库的连接与修改 OleDbConnection
mysword
2004.10.30
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;namespace CustomerEditor
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu menuMain;
private System.Windows.Forms.MenuItem menuData;
private System.Windows.Forms.MenuItem menuDataConnect;
private System.Windows.Forms.OpenFileDialog dialogOpenFile;
private OleDbConnection _connection;
private System.Windows.Forms.MenuItem menuDataLoad;
private System.Windows.Forms.DataGrid dataGridCustomers;
private System.Windows.Forms.MenuItem menuDataSaveChanges;
private System.Windows.Forms.MenuItem menuExit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
Disconnect();
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.menuMain = new System.Windows.Forms.MainMenu();
this.menuData = new System.Windows.Forms.MenuItem();
this.menuDataConnect = new System.Windows.Forms.MenuItem();
this.dialogOpenFile = new System.Windows.Forms.OpenFileDialog();
this.dataGridCustomers = new System.Windows.Forms.DataGrid();
this.menuDataLoad = new System.Windows.Forms.MenuItem();
this.menuDataSaveChanges = new System.Windows.Forms.MenuItem();
this.menuExit = new System.Windows.Forms.MenuItem();
((System.ComponentModel.ISupportInitialize)(this.dataGridCustomers)).BeginInit();
this.SuspendLayout();
//
// menuMain
//
this.menuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuData});
//
// menuData
//
this.menuData.Index = 0;
this.menuData.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuDataConnect,
this.menuDataLoad,
this.menuDataSaveChanges,
this.menuExit});
this.menuData.Text = "数据库";
//
// menuDataConnect
//
this.menuDataConnect.Index = 0;
this.menuDataConnect.Text = "连接数据库";
this.menuDataConnect.Click += new System.EventHandler(this.menuDataConnect_Click);
//
// dataGridCustomers
//
this.dataGridCustomers.DataMember = "";
this.dataGridCustomers.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridCustomers.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridCustomers.Location = new System.Drawing.Point(0, 0);
this.dataGridCustomers.Name = "dataGridCustomers";
this.dataGridCustomers.Size = new System.Drawing.Size(792, 553);
this.dataGridCustomers.TabIndex = 0;
//
// menuDataLoad
//
this.menuDataLoad.Index = 1;
this.menuDataLoad.Text = "加载数据表";
this.menuDataLoad.Click += new System.EventHandler(this.menuDataLoad_Click);
//
// menuDataSaveChanges
//
this.menuDataSaveChanges.Index = 2;
this.menuDataSaveChanges.Text = "保存修改";
this.menuDataSaveChanges.Click += new System.EventHandler(this.menuDataSaveChanges_Click);
//
// menuExit
//
this.menuExit.Index = 3;
this.menuExit.Text = "退出";
this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(792, 553);
this.Controls.Add(this.dataGridCustomers);
this.Menu = this.menuMain;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "数据库操作(OleDbConnection)";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
((System.ComponentModel.ISupportInitialize)(this.dataGridCustomers)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void menuDataConnect_Click(object sender, System.EventArgs e)
{
Connect();
} public void Connect()
{
if(dialogOpenFile.ShowDialog(this)==DialogResult.OK)
{
try
{
string connectionString=string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",
dialogOpenFile.FileName); OleDbConnection newConnection=
new OleDbConnection(connectionString); newConnection.Open(); Connection=newConnection;
} catch(Exception ex)
{
HandleException("a connection could not be made.",ex);
}
}
}
public void HandleException(string message,Exception ex)
{
MessageBox.Show(this,string.Format("{0}\n{1}:{2}",
message,ex.GetType().ToString(),ex.Message));
} public OleDbConnection Connection
{
get
{
return _connection;
} set
{
Disconnect();
_connection=value;
}
}
public void Disconnect()
{
if(_connection!=null)
{
if(_connection.State!=ConnectionState.Closed)
_connection.Close(); _connection=null;
}
} private void menuDataLoad_Click(object sender, System.EventArgs e)
{
LoadData();
} public void LoadData()
{
if(_connection==null)
{
MessageBox.Show(this,"you must connect to a database.");
return;
} OleDbCommand command=null;
OleDbDataAdapter adapter=null; try
{
command=_connection.CreateCommand(); command.CommandText="newpost";//表的指定
command.CommandType=CommandType.TableDirect; adapter=new OleDbDataAdapter(command); DataSet dataset=new DataSet();
adapter.Fill(dataset); dataGridCustomers.DataSource=dataset; }
catch(Exception ex)
{
HandleException("the data could not be loaded.",ex);
} finally
{
if(adapter!=null)
adapter.Dispose();
if(command!=null)
command.Dispose();
}
} private void menuExit_Click(object sender, System.EventArgs e)
{
this.Close();
} private void menuDataSaveChanges_Click(object sender, System.EventArgs e)
{
SaveChanges();
}
public void SaveChanges()
{
if(_connection==null)
{
MessageBox.Show("you must connect to a database.");
return;
} DataSet dataset=(DataSet)dataGridCustomers.DataSource;
if(dataset==null)
{
MessageBox.Show("you must load a dataset.");
return;
} OleDbCommand command=null;
OleDbDataAdapter adapter=null; try
{
command=_connection.CreateCommand(); command.CommandText="newpost";//表的指定
command.CommandType=CommandType.TableDirect; adapter=new OleDbDataAdapter(command); OleDbCommandBuilder builder=new OleDbCommandBuilder(adapter); adapter.Update(dataset); MessageBox.Show("changes have been saved."); } finally
{
if(adapter!=null)
adapter.Dispose();
if(command!=null)
command.Dispose();
}
}
}
}===================================