views:

127

answers:

0

hi, I am currently working in application,I loaded MS_access database into datagridview there i have written code for insert,delete,update and search .

For Example: IN Ms Word when we click CTl+Z any previously made changes would be restored. Just like this any previous changes made in the datagridview should be restored using either button or CTRL+Z keys.

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;
    }           
}

}

The above code clearly demonstrates the usage of update,delete,search and insert options in datagridview but i dont no how i should proceed with undo operation.

Can anyone help me on this.