Delete data and reload to DataGrid : OleDbCommandBuilder « ADO.Net « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » ADO.Net » OleDbCommandBuilder 
27. 8. 3. Delete data and reload to DataGrid
Delete data and reload to DataGrid
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

public class DeleteDataLoadDataGrid : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.Button InsertCommand;
  private System.Windows.Forms.Button UpdateCommand;
  private System.Windows.Forms.Button DeleteCommand;
  private System.Windows.Forms.CheckBox checkBox1;

  private System.ComponentModel.Container components = null;

  public DeleteDataLoadDataGrid()
  {
    InitializeComponent();

  }

  protected override void Disposebool disposing )
  {
    ifdisposing )
    {
      if (components != null
      {
        components.Dispose();
      }
    }
    base.Disposedisposing );
  }

  private void InitializeComponent()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.InsertCommand = new System.Windows.Forms.Button();
    this.UpdateCommand = new System.Windows.Forms.Button();
    this.DeleteCommand = new System.Windows.Forms.Button();
    this.checkBox1 = new System.Windows.Forms.CheckBox();
    ((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(88);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(336304);
    this.dataGrid1.TabIndex = 0;
    // 
    // InsertCommand
    // 
    this.InsertCommand.Location = new System.Drawing.Point(36824);
    this.InsertCommand.Name = "InsertCommand";
    this.InsertCommand.Size = new System.Drawing.Size(12032);
    this.InsertCommand.TabIndex = 1;
    this.InsertCommand.Text = "Insert Command";
    this.InsertCommand.Click += new System.EventHandler(this.InsertCommand_Click);
    // 
    // UpdateCommand
    // 
    this.UpdateCommand.Location = new System.Drawing.Point(36872);
    this.UpdateCommand.Name = "UpdateCommand";
    this.UpdateCommand.Size = new System.Drawing.Size(12032);
    this.UpdateCommand.TabIndex = 2;
    this.UpdateCommand.Text = "Update Command";
    this.UpdateCommand.Click += new System.EventHandler(this.UpdateCommand_Click);
    // 
    // DeleteCommand
    // 
    this.DeleteCommand.Location = new System.Drawing.Point(368120);
    this.DeleteCommand.Name = "DeleteCommand";
    this.DeleteCommand.Size = new System.Drawing.Size(12032);
    this.DeleteCommand.TabIndex = 3;
    this.DeleteCommand.Text = "Delete Command";
    this.DeleteCommand.Click += new System.EventHandler(this.DeleteCommand_Click);
    // 
    // checkBox1
    // 
    this.checkBox1.Location = new System.Drawing.Point(368192);
    this.checkBox1.Name = "checkBox1";
    this.checkBox1.Size = new System.Drawing.Size(11224);
    this.checkBox1.TabIndex = 4;
    this.checkBox1.Text = "SqlCommand";
    // 
    // DeleteDataLoadDataGrid
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(496325);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.checkBox1,
                                    this.DeleteCommand,
                                    this.UpdateCommand,
                                    this.InsertCommand,
                                    this.dataGrid1});
    this.Name = "DeleteDataLoadDataGrid";
    this.Text = "DeleteDataLoadDataGrid";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);

  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new DeleteDataLoadDataGrid());
  }

  private void InsertCommand_Click(object sender, System.EventArgs e)
    {
    }

  private void UpdateCommand_Click(object sender, System.EventArgs e)
    {
    }


  private void DeleteCommand_Click(object sender, System.EventArgs e)
    {
        
    string ConnectionString = 
      "Provider=Microsoft.Jet.OLEDB.4.0;"+
      "Data Source=c:\\Northwind.mdb";
    OleDbConnection conn = 
      new OleDbConnection(ConnectionString);
    DataSet ds = new DataSet();
        
    try
    {
      conn.Open();

      OleDbDataAdapter adapter = new OleDbDataAdapter(
        "SELECT * FROM Customers", conn);
            
      OleDbCommandBuilder cmdBuilder = 
        new OleDbCommandBuilder(adapter);
      adapter.MissingSchemaAction = 
        MissingSchemaAction.AddWithKey;
    
      adapter.Fill(ds, "Customers");
    
      DataRow row1 = ds.Tables["Customers"].Rows.Find("001");
      row1.Delete()
      adapter.Update(ds, "Customers");
      dataGrid1.DataSource = ds.DefaultViewManager;
    }
    catch(OleDbException exp)
    {
      MessageBox.Show(exp.Message.ToString());
    }

    if(conn.State == ConnectionState.Open)
      conn.Close();     
    }
}
27. 8. OleDbCommandBuilder
27. 8. 1. Insert Data and reload to DataGridInsert Data and reload to DataGrid
27. 8. 2. Update data and reload to DataGridUpdate data and reload to DataGrid
27. 8. 3. Delete data and reload to DataGridDelete data and reload to DataGrid
w_w___w__.___j___a__va__2___s_.c___om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.