DataGrid Update: edit a table by binding component : Data Bind DataGrid « Database ADO.net « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
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
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Database ADO.net » Data Bind DataGridScreenshots 
DataGrid Update: edit a table by binding component

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

    public class Form1 : System.Windows.Forms.Form {
      private System.Windows.Forms.DataGrid dataGrid1;
      private System.Windows.Forms.Button buttonUpdate;
      private System.Data.DataSet dataSet1;
      private System.Data.SqlClient.SqlCommand sqlCommand1;
      private System.ComponentModel.Container components = null;

      private SqlCommandBuilder cb;
      private SqlDataAdapter da;

      public Form1() {
         InitializeComponent();
      }
      private void InitializeComponent(){
         this.dataGrid1 = new System.Windows.Forms.DataGrid();
         this.buttonUpdate = new System.Windows.Forms.Button();
         this.dataSet1 = new System.Data.DataSet();
         this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
         ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
         this.SuspendLayout();

         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(440208);
         this.dataGrid1.TabIndex = 0;

         this.buttonUpdate.Location = new System.Drawing.Point(191232);
         this.buttonUpdate.Name = "buttonUpdate";
         this.buttonUpdate.TabIndex = 1;
         this.buttonUpdate.Text = "Update";
         this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click);

         this.dataSet1.DataSetName = "NewDataSet";
         this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");

         this.AutoScaleBaseSize = new System.Drawing.Size(513);
         this.ClientSize = new System.Drawing.Size(456272);
         this.Controls.Add(this.buttonUpdate);
         this.Controls.Add(this.dataGrid1);
         this.Name = "Form1";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.Form1_Load);
         ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
         this.ResumeLayout(false);

      }
      static void Main() {
         Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e){
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string sql = @"select * from employee ";
         SqlConnection conn = new SqlConnection(connString);
         sqlCommand1 = new SqlCommand(sql, conn);

         da = new SqlDataAdapter();
         da.SelectCommand = sqlCommand1;

         cb = new SqlCommandBuilder(da);

         da.Fill(dataSet1, "employee");

         dataGrid1.SetDataBinding(dataSet1, "employee");
      }

      private void buttonUpdate_Click(object sender, System.EventArgs e) {
         da.Update(dataSet1, "employee")
      }
    }



           
       
Related examples in the same category
1. Bind DataSet to DataGrid
2. Bind two DataGrids with many to many mapping
w__ww__.___j_a__v_a_2__s.___co_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.