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

Home
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.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » OleDbCommandBuilder 
32.9.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();     
    }
}
32.9.OleDbCommandBuilder
32.9.1.Insert Data and reload to DataGridInsert Data and reload to DataGrid
32.9.2.Update data and reload to DataGridUpdate data and reload to DataGrid
32.9.3.Delete data and reload to DataGridDelete data and reload to DataGrid
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.