using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace dataGridViewUse
{
public partial class Form4 : Form
{
private DataSet ds = new DataSet();
public OleDbDataAdapter ad = new OleDbDataAdapter();
private OleDbCommandBuilder ocb = new OleDbCommandBuilder();
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
disPlay();
}
//显示表里的数据
public void disPlay()
{
// OleDbConnection myCon = new OleDbConnection("Provider=SQLOLEDB.1;Data Source=SDL;Persist Security Info=True;Password=1234;User ID=sa;Initial Catalog=ABC");
OleDbConnection myCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/My Documents/Visual Studio 2005/Projects/dataGridViewUse/dataGridViewUse/表.mdb");
ad = new OleDbDataAdapter("select * from Area", myCon);
ocb = new OleDbCommandBuilder(ad);
ds = new DataSet();
ad.Fill(ds, "Area");
ocb = new OleDbCommandBuilder(ad);//系统自动生产insert,add,update,del语句,切记设置表的主键
dataGridView1.DataSource = ds.Tables[0];
}
//保存datagridview里的记录
private void BtnEdit_Click(object sender, EventArgs e)
{
try
{
ad.Update(ds.Tables[0]);
ds.Tables["Area"].AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void BtnFrush_Click(object sender, EventArgs e)
{
ds.Tables[0].AcceptChanges();
}
//增加一个空行
private void btnAdd_Click(object sender, EventArgs e)
{
//dataGridView1.AllowUserToAddRows = true;
DataRow dtrow = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dtrow);
dataGridView1.DataSource = ds.Tables[0];
}
}
}