我目前正在申请中,我将MS_access数据库加载到datagridview中,我已经编写了插入,删除,更新和搜索的代码。
例如:当我们单击CTl + Z时,在Ms Ms中,将恢复之前所做的任何更改。就像这样,应该使用按钮或CTRL + Z键恢复datagridview中所做的任何先前更改。
public partial class Form2 : Form
{
OleDbConnection con;
OleDbDataAdapter da;
DataTable dt;
OleDbCommand cm;
private void button26_Click(object sender, EventArgs e)//UPDATE OPERATION
{
da.Update(dt);
da = new OleDbDataAdapter("select * from Contacts", con);
DataSet ds = new DataSet();
da.Fill(ds, "Contacts");
FileStream book = new FileStream("softphone contacts.xml",
FileMode.Create,
FileAccess.Write);
ds.WriteXml(book);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}
private void button27_Click(object sender, EventArgs e)//SEARCH
{
string searchvalue;
searchvalue = textBox3.Text;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/ppd/db2.mdb;Jet OLEDB:Database Password=techsoft");
da = new OleDbDataAdapter("select * from Contacts", con);
DataSet ds = new DataSet();
da.Fill(ds, "Contacts");
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "FirstName='" + searchvalue + "' or LastName like '%" + searchvalue + "' or WorkPhone like '%" + searchvalue + "%' ";
ds.Tables.Clear();
ds.Tables.Add(dv.ToTable());
ds.AcceptChanges();
da.Update(dt);
}
private void Form2_Load(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.SteelBlue;
dataGridView1.Visible = true;
//button25.Visible = true;
button26.Visible = true;
button27.Visible = true;
textBox3.Visible = true;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/ppd/db2.mdb;Jet OLEDB:Database Password=techsoft");
da = new OleDbDataAdapter("select * from Contacts", con);
DataSet ds = new DataSet();
da.Fill(ds, "Contacts");
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
string insertQuery = @"insert into Contacts(FirstName,LastName,WorkPhone,ContactsID) Values (@FirstName,@LastName,@PhoneNumber,@no)";
cm = new OleDbCommand(insertQuery, con);
cm.Parameters.Add("@FirstName", OleDbType.VarChar, 10, "FirstName");
cm.Parameters.Add("@LastName", OleDbType.VarChar, 10, "LastName");
cm.Parameters.Add("@WorkPhone", OleDbType.VarChar, 10, "WorkPhone");
cm.Parameters.Add("@ContactsID", OleDbType.VarChar, 10, "ContactsID");
da.InsertCommand = cm;
string update = @"update Contacts set FirstName=@FirstName,LastName=@LastName,WorkPhone=@WorkPhone where(ContactsID=@ContactsID) ";
cm = new OleDbCommand(update, con);
cm.Parameters.Add("@FirstName", OleDbType.VarChar, 10, "FirstName");
cm.Parameters.Add("@LastName", OleDbType.VarChar, 10, "LastName");
cm.Parameters.Add("@WorkPhone", OleDbType.VarChar, 10, "WorkPhone");
cm.Parameters.Add("@ContactsID", OleDbType.VarChar, 10, "ContactsID");
da.UpdateCommand = cm;
string delete = @"delete from Contacts where FirstName=@FirstName ";
cm = new OleDbCommand(delete, con);
cm.Parameters.Add("@FirstName", OleDbType.VarChar, 10, "FirstName");
da.DeleteCommand = cm;
}
}
}
以上代码清楚地演示了datagridview中更新,删除,搜索和插入选项的用法,但我不知道如何继续进行撤销操作。
任何人都可以帮我这个。