Microsoft KB Archive/833032

= BUG: The last-modified cell of a DataGrid control loses changes when you close a form =

Article ID: 833032

Article Last Modified on 4/29/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Academic Edition

-





SYMPTOMS
When you close a form that contains a DataGrid control as the active control without moving the focus from the last cell that you edited, you lose the changes that you made to the last cell that you edited.



CAUSE
Because the DataGrid control is not a container control, the DataGrid control does not raise the Leave event. Therefore, the DataGrid control does not update the corresponding database with the changes that you made to the last cell that you edited, and you notice the behavior that is mentioned in the &quot;Symptoms&quot; section.



WORKAROUND
To work around this bug, follow these steps:  Start Microsoft Visual Studio .NET Open the form that contains the DataGrid control.  In the Form1_Closing event handler, add the following code before the code that updates your database with the changes that you made: DataGridColumnStyle cStyle = dataGrid2.TableStyles[dvOrderDetails.Table.TableName].GridColumnStyles[dataGrid2.CurrentCell.ColumnNumber]; dataGrid2.EndEdit(cStyle, dataGrid2.CurrentCell.RowNumber, false); dvOrderDetails.Table.Rows[dataGrid2.CurrentCell.RowNumber].EndEdit; 



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.



Steps to reproduce the behavior
 Start Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> Click Visual C# Projects under Project Types, and then click Windows Application under Templates.</li> In the Location text box, type C:\.

Note is a placeholder for the name of the folder where you want to save your application.</li> In the Name text box, type GridApplication1 as the name of your application.</li> Click OK. By default, the Form1.cs file is created.</li> Right-click Form1, and then click View Code.</li>  Replace the existing code in the Form1.cs file with the following code.

Note In the following code, change the connection string to an appropriate value for your database. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace GridApplication1 {   ///     /// Summary description for Form1. ///    public class Form1 : System.Windows.Forms.Form {       private System.Data.OleDb.OleDbDataAdapter daOrder; private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Data.OleDb.OleDbDataAdapter daOrderDetails; private System.Data.OleDb.OleDbCommand oleDbSelectCommand2; private System.Data.OleDb.OleDbCommand oleDbUpdateCommand2; private GridApplication1.OrderDataSet orderDataSet1; private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.DataGrid dataGrid2; private System.Data.DataView dvOrder; private System.Data.DataView dvOrderDetails; private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1; private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1; private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2; private System.Windows.Forms.DataGridTableStyle dataGridTableStyle2; private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3; private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4; ///        /// Required designer variable. ///        private System.ComponentModel.Container components = null;

public Form1 {           //            // Required for Windows Form Designer support //           InitializeComponent;

//           // TODO: Add any constructor code after InitializeComponent call //       }

///        /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

private GridApplication1.OrderDataSet GetOrderData {           GridApplication1.OrderDataSet ds = new GridApplication1.OrderDataSet; this.daOrder.Fill(ds); this.daOrderDetails.Fill(ds);

return ds; }       public void UpdateOrderData(GridApplication1.OrderDataSet dsChanges) {           this.daOrder.Update(dsChanges); this.daOrderDetails.Update(dsChanges); }

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {           this.daOrder = new System.Data.OleDb.OleDbDataAdapter; this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection; this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand; this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand; this.daOrderDetails = new System.Data.OleDb.OleDbDataAdapter; this.oleDbSelectCommand2 = new System.Data.OleDb.OleDbCommand; this.oleDbUpdateCommand2 = new System.Data.OleDb.OleDbCommand; this.orderDataSet1 = new GridApplication1.OrderDataSet; this.dataGrid1 = new System.Windows.Forms.DataGrid; this.dvOrder = new System.Data.DataView; this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle; this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn; this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn; this.dataGrid2 = new System.Windows.Forms.DataGrid; this.dvOrderDetails = new System.Data.DataView; this.dataGridTableStyle2 = new System.Windows.Forms.DataGridTableStyle; this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn; this.dataGridTextBoxColumn4 = new System.Windows.Forms.DataGridTextBoxColumn; ((System.ComponentModel.ISupportInitialize)(this.orderDataSet1)).BeginInit; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit; ((System.ComponentModel.ISupportInitialize)(this.dvOrder)).BeginInit; ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit; ((System.ComponentModel.ISupportInitialize)(this.dvOrderDetails)).BeginInit; this.SuspendLayout; //            // daOrder //            this.daOrder.SelectCommand = this.oleDbSelectCommand1; this.daOrder.TableMappings.AddRange(new System.Data.Common.DataTableMapping[]      {        new System.Data.Common.DataTableMapping(&quot;Table&quot;, &quot;Orders&quot;, new System.Data.Common.DataColumnMapping[] {   new System.Data.Common.DataColumnMapping(&quot;OrderID&quot;, &quot;OrderID&quot;), new System.Data.Common.DataColumnMapping(&quot;CustomerID&quot;, &quot;CustomerID&quot;), new System.Data.Common.DataColumnMapping(&quot;EmployeeID&quot;, &quot;EmployeeID&quot;), new System.Data.Common.DataColumnMapping(&quot;OrderDate&quot;, &quot;OrderDate&quot;), new System.Data.Common.DataColumnMapping(&quot;RequiredDate&quot;, &quot;RequiredDate&quot;), new System.Data.Common.DataColumnMapping(&quot;ShippedDate&quot;, &quot;ShippedDate&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipVia&quot;, &quot;ShipVia&quot;), new System.Data.Common.DataColumnMapping(&quot;Freight&quot;, &quot;Freight&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipName&quot;, &quot;ShipName&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipAddress&quot;, &quot;ShipAddress&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipCity&quot;, &quot;ShipCity&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipRegion&quot;, &quot;ShipRegion&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipPostalCode&quot;, &quot;ShipPostalCode&quot;), new System.Data.Common.DataColumnMapping(&quot;ShipCountry&quot;, &quot;ShipCountry&quot;)})}); this.daOrder.UpdateCommand = this.oleDbUpdateCommand1; //            // oleDbConnection1 //    this.oleDbConnection1.ConnectionString = @&quot;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind; Data Source=mdpkb2e141;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096; Workstation ID=mdpkb2e141; Use Encryption for Data=False;Tag with column collation when possible=False&quot;;

//    // oleDbSelectCommand1 //    this.oleDbSelectCommand1.CommandText = &quot;SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, Shi&quot; + &quot;pVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, Ship&quot; + &quot;Country FROM Orders&quot;; this.oleDbSelectCommand1.Connection = this.oleDbConnection1; //            // oleDbUpdateCommand1 //    this.oleDbUpdateCommand1.CommandText = @&quot;UPDATE Orders SET CustomerID = ?, EmployeeID = ?, OrderDate = ?, RequiredDate = ?, ShippedDate = ?, ShipVia = ?, Freight = ?, ShipName = ?, ShipAddress = ?, ShipCity = ?, ShipRegion = ?, ShipPostalCode = ?, ShipCountry = ? WHERE (OrderID = ?) AND (CustomerID = ? OR ? IS NULL AND CustomerID IS NULL) AND (EmployeeID = ? OR ? IS NULL AND EmployeeID IS NULL) AND (Freight = ? OR ? IS NULL AND Freight IS NULL) AND (OrderDate = ? OR ? IS NULL AND OrderDate IS NULL) AND (RequiredDate = ? OR ? IS NULL    AND RequiredDate IS NULL) AND (ShipAddress = ? OR ? IS NULL AND ShipAddress IS NULL) AND (ShipCity = ? OR ? IS NULL AND ShipCity IS NULL) AND (ShipCountry = ? OR ?    IS NULL AND ShipCountry IS NULL) AND (ShipName = ? OR ? IS NULL AND ShipName IS NULL) AND (ShipPostalCode = ? OR ? IS NULL AND ShipPostalCode IS NULL) AND (ShipRegion = ? OR ?    IS NULL AND ShipRegion IS NULL) AND (ShipVia = ? OR ? IS NULL AND ShipVia IS NULL) AND (ShippedDate = ? OR ? IS NULL AND ShippedDate IS NULL); SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry FROM Orders WHERE (OrderID = ?)&quot;; this.oleDbUpdateCommand1.Connection = this.oleDbConnection1; this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;CustomerID&quot;, System.Data.OleDb.OleDbType.VarWChar, 5, &quot;CustomerID&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;EmployeeID&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;EmployeeID&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;OrderDate&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, &quot;OrderDate&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;RequiredDate&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, &quot;RequiredDate&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShippedDate&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, &quot;ShippedDate&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipVia&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;ShipVia&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Freight&quot;, System.Data.OleDb.OleDbType.Currency, 8, &quot;Freight&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipName&quot;, System.Data.OleDb.OleDbType.VarWChar, 40, &quot;ShipName&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipAddress&quot;, System.Data.OleDb.OleDbType.VarWChar, 60, &quot;ShipAddress&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipCity&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, &quot;ShipCity&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipRegion&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, &quot;ShipRegion&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipPostalCode&quot;, System.Data.OleDb.OleDbType.VarWChar, 10, &quot;ShipPostalCode&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ShipCountry&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, &quot;ShipCountry&quot;)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_OrderID&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;OrderID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter       (&quot;Original_CustomerID&quot;, System.Data.OleDb.OleDbType.VarWChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;CustomerID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_CustomerID1&quot;, System.Data.OleDb.OleDbType.VarWChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;CustomerID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_EmployeeID&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;EmployeeID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_EmployeeID1&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;EmployeeID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_Freight&quot;, System.Data.OleDb.OleDbType.Currency, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;Freight&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_Freight1&quot;, System.Data.OleDb.OleDbType.Currency, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;Freight&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_OrderDate&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;OrderDate&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter       (&quot;Original_OrderDate1&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;OrderDate&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_RequiredDate&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;RequiredDate&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_RequiredDate1&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;RequiredDate&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipAddress&quot;, System.Data.OleDb.OleDbType.VarWChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipAddress&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipAddress1&quot;, System.Data.OleDb.OleDbType.VarWChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipAddress&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipCity&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipCity&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipCity1&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipCity&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipCountry&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipCountry&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipCountry1&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipCountry&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipName&quot;, System.Data.OleDb.OleDbType.VarWChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipName&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipName1&quot;, System.Data.OleDb.OleDbType.VarWChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipName&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter       (&quot;Original_ShipPostalCode&quot;, System.Data.OleDb.OleDbType.VarWChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipPostalCode&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipPostalCode1&quot;, System.Data.OleDb.OleDbType.VarWChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipPostalCode&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipRegion&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipRegion&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipRegion1&quot;, System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipRegion&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipVia&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipVia&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShipVia1&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShipVia&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShippedDate&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShippedDate&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ShippedDate1&quot;, System.Data.OleDb.OleDbType.DBTimeStamp, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ShippedDate&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter(&quot;Select_OrderID&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;OrderID&quot;)); //    // daOrderDetails //

this.daOrderDetails.SelectCommand = this.oleDbSelectCommand2; this.daOrderDetails.TableMappings.AddRange(new System.Data.Common.DataTableMapping[]    {    new System.Data.Common.DataTableMapping(&quot;Table&quot;, &quot;Order Details&quot;, new System.Data.Common.DataColumnMapping[] {   new System.Data.Common.DataColumnMapping(&quot;OrderID&quot;, &quot;OrderID&quot;), new System.Data.Common.DataColumnMapping(&quot;ProductID&quot;, &quot;ProductID&quot;), new System.Data.Common.DataColumnMapping(&quot;UnitPrice&quot;, &quot;UnitPrice&quot;), new System.Data.Common.DataColumnMapping(&quot;Quantity&quot;, &quot;Quantity&quot;), new System.Data.Common.DataColumnMapping(&quot;Discount&quot;, &quot;Discount&quot;)})}); this.daOrderDetails.UpdateCommand = this.oleDbUpdateCommand2;

//         //     // oleDbSelectCommand2 //    this.oleDbSelectCommand2.CommandText = &quot;SELECT OrderID, ProductID, UnitPrice, Quantity, Discount FROM [Order Details]&quot;; this.oleDbSelectCommand2.Connection = this.oleDbConnection1; //    // oleDbUpdateCommand2 //    this.oleDbUpdateCommand2.CommandText = @&quot;UPDATE [Order Details] SET OrderID = ?, ProductID = ?, UnitPrice = ?, Quantity = ?, Discount = ? WHERE (OrderID = ?) AND (ProductID = ?) AND (Discount = ?) AND (Quantity = ?) AND (UnitPrice = ?); SELECT OrderID, ProductID, UnitPrice, Quantity, Discount FROM [Order Details] WHERE (OrderID = ?) AND (ProductID = ?)&quot;; this.oleDbUpdateCommand2.Connection = this.oleDbConnection1; this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;OrderID&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;OrderID&quot;)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;ProductID&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;ProductID&quot;)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;UnitPrice&quot;, System.Data.OleDb.OleDbType.Currency, 8, &quot;UnitPrice&quot;)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Quantity&quot;, System.Data.OleDb.OleDbType.SmallInt, 2, &quot;Quantity&quot;)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Discount&quot;, System.Data.OleDb.OleDbType.Single, 4, &quot;Discount&quot;)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_OrderID&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;OrderID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_ProductID&quot;, System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;ProductID&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_Discount&quot;, System.Data.OleDb.OleDbType.Single, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;Discount&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Original_Quantity&quot;, System.Data.OleDb.OleDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;Quantity&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter       (&quot;Original_UnitPrice&quot;, System.Data.OleDb.OleDbType.Currency, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), &quot;UnitPrice&quot;, System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand2.Parameters.Add (new System.Data.OleDb.OleDbParameter(&quot;Select_OrderID&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;OrderID&quot;)); this.oleDbUpdateCommand2.Parameters.Add(new System.Data.OleDb.OleDbParameter       (&quot;Select_ProductID&quot;, System.Data.OleDb.OleDbType.Integer, 4, &quot;ProductID&quot;)); //            // orderDataSet1 //            this.orderDataSet1.DataSetName = &quot;OrderDataSet&quot;; this.orderDataSet1.Locale = new System.Globalization.CultureInfo(&quot;en-US&quot;); //            // dataGrid1 //            this.dataGrid1.DataMember = &quot;&quot;; this.dataGrid1.DataSource = this.dvOrder; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 16); this.dataGrid1.Name = &quot;dataGrid1&quot;; this.dataGrid1.Size = new System.Drawing.Size(280, 288); this.dataGrid1.TabIndex = 0; this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[]            {            this.dataGridTableStyle1}); this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged); //            // dvOrder //            this.dvOrder.AllowNew = false; this.dvOrder.Table = this.orderDataSet1.Orders; //            // dataGridTableStyle1 //            this.dataGridTableStyle1.DataGrid = this.dataGrid1; this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[]             {          this.dataGridTextBoxColumn1,          this.dataGridTextBoxColumn2}); this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridTableStyle1.MappingName = &quot;Orders&quot;; this.dataGridTableStyle1.RowHeadersVisible = false; //            // dataGridTextBoxColumn1 //            this.dataGridTextBoxColumn1.Format = &quot;&quot;; this.dataGridTextBoxColumn1.FormatInfo = null; this.dataGridTextBoxColumn1.HeaderText = &quot;OrderID&quot;; this.dataGridTextBoxColumn1.MappingName = &quot;OrderID&quot;; this.dataGridTextBoxColumn1.ReadOnly = true; this.dataGridTextBoxColumn1.Width = 75; //            // dataGridTextBoxColumn2 //            this.dataGridTextBoxColumn2.Format = &quot;&quot;; this.dataGridTextBoxColumn2.FormatInfo = null; this.dataGridTextBoxColumn2.HeaderText = &quot;Ship City&quot;; this.dataGridTextBoxColumn2.MappingName = &quot;ShipCity&quot;; this.dataGridTextBoxColumn2.Width = 75; //            // dataGrid2 //            this.dataGrid2.DataMember = &quot;&quot;; this.dataGrid2.DataSource = this.dvOrderDetails; this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid2.Location = new System.Drawing.Point(312, 16); this.dataGrid2.Name = &quot;dataGrid2&quot;; this.dataGrid2.Size = new System.Drawing.Size(248, 288); this.dataGrid2.TabIndex = 1; this.dataGrid2.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[]      {          this.dataGridTableStyle2}); //            // dvOrderDetails //            this.dvOrderDetails.AllowNew = false; this.dvOrderDetails.Table = this.orderDataSet1.Order_Details; //            // dataGridTableStyle2 //            this.dataGridTableStyle2.DataGrid = this.dataGrid2; this.dataGridTableStyle2.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[]      {          this.dataGridTextBoxColumn3,          this.dataGridTextBoxColumn4}); this.dataGridTableStyle2.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridTableStyle2.MappingName = &quot;Order Details&quot;; this.dataGridTableStyle2.RowHeadersVisible = false; //            // dataGridTextBoxColumn3 //            this.dataGridTextBoxColumn3.Format = &quot;&quot;; this.dataGridTextBoxColumn3.FormatInfo = null; this.dataGridTextBoxColumn3.HeaderText = &quot;Product ID&quot;; this.dataGridTextBoxColumn3.MappingName = &quot;ProductID&quot;; this.dataGridTextBoxColumn3.ReadOnly = true; this.dataGridTextBoxColumn3.Width = 75; //            // dataGridTextBoxColumn4 //            this.dataGridTextBoxColumn4.Format = &quot;&quot;; this.dataGridTextBoxColumn4.FormatInfo = null; this.dataGridTextBoxColumn4.HeaderText = &quot;Quantity&quot;; this.dataGridTextBoxColumn4.MappingName = &quot;Quantity&quot;; this.dataGridTextBoxColumn4.Width = 75; //            // Form1 //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(584, 363); this.Controls.Add(this.dataGrid2); this.Controls.Add(this.dataGrid1); this.Name = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.orderDataSet1)).EndInit; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit; ((System.ComponentModel.ISupportInitialize)(this.dvOrder)).EndInit; ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit; ((System.ComponentModel.ISupportInitialize)(this.dvOrderDetails)).EndInit; this.ResumeLayout(false);

}   #endregion

///        /// The main entry point for the application. ///        [STAThread] static void Main {           Application.Run(new MainForm); }

private void Form1_Load(object sender, System.EventArgs e)       { bool MaintainChanges = true; GridApplication1.OrderDataSet ds = new GridApplication1.OrderDataSet; ds = this.GetOrderData; this.orderDataSet1.Merge(ds,MaintainChanges); this.FilterOrderDetialGrid; }

private void FilterOrderDetialGrid {           if(this.BindingContext[this.dvOrder].Count > 0) {               DataRowView drv = (DataRowView)this.BindingContext[this.dvOrder].Current; string OrderID = drv[&quot;OrderID&quot;].ToString; this.dvOrderDetails.RowFilter = &quot; OrderID = &quot; + OrderID; }       }

private void SaveChanges {           // On Tab out of the dataGrid Cell after you edit it, // the following code helps to update the Database. this.BindingContext[this.dvOrder].EndCurrentEdit; this.BindingContext[this.dvOrderDetails].EndCurrentEdit;

GridApplication1.OrderDataSet dsChanges = (GridApplication1.OrderDataSet)this.orderDataSet1.GetChanges; if(dsChanges != null) {               try {                   this.UpdateOrderData(dsChanges); }               catch(Exception eUpdate) {                   string ErrMsg = eUpdate.ToString; }               this.orderDataSet1.AcceptChanges; }       }

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)       { this.FilterOrderDetialGrid; }

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)       { this.SaveChanges; }

} } </li> Add a form that is named MainForm to the project. To do this, follow these steps:  In Solution Explorer, right-click GridApplication1.</li> Point to Add, and then click Add New Item. The Add New Item - GridApplication1 dialog box appears.</li> Under Templates, click Windows Form.</li> In the Name text box, type MainForm.cs .</li> Click Open.</li></ol> </li> Right-click MainForm, and then click View Code.</li>  Replace the existing code in the MainForm.cs file with the following code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;

namespace GridApplication1 {   ///     /// Summary description for MainForm. ///    public class MainForm : System.Windows.Forms.Form {       private System.Windows.Forms.Button button1; ///        /// Required designer variable. ///        private System.ComponentModel.Container components = null;

public MainForm {           //            // Required for Windows Form Designer support //           InitializeComponent;

//           // TODO: Add any constructor code after InitializeComponent call //       }

///        /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if( disposing ) {               if(components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {           this.button1 = new System.Windows.Forms.Button; this.SuspendLayout; //            // button1 //            this.button1.Location = new System.Drawing.Point(104, 104); this.button1.Name = &quot;button1&quot;; this.button1.TabIndex = 0; this.button1.Text = &quot;View Orders&quot;; this.button1.Click += new System.EventHandler(this.button1_Click); //            // MainForm //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 271); this.Controls.AddRange(new System.Windows.Forms.Control[] {                                                                         this.button1}); this.Name = &quot;MainForm&quot;; this.Text = &quot;MainForm&quot;; this.ResumeLayout(false);

}   #endregion

private void button1_Click(object sender, System.EventArgs e)       { GridApplication1.Form1 OrderForm = new GridApplication1.Form1; OrderForm.Show; }   } }

</li> <li>Add a DataSet control to the project. To add a DataSet control, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In Solution Explorer, right-click GridApplication1.</li> <li>Point to Add, and then click Add New Item. The Add New Item - GridApplication1 dialog box appears.</li> <li>Under Templates, click Data Set.</li> <li>In the Name text box, type OrderDataSet.xsd .</li> <li>Click Open.</li></ol> </li> <li>Switch to the XML view of the OrderDataSet data set.</li> <li> In the OrderDataSet.xsd file, replace the existing code with the following code: <?xml version=&quot;1.0&quot; standalone=&quot;yes&quot; ?> <xs:schema id=&quot;OrderDataSet&quot; targetNamespace=&quot;http://www.tempuri.org/OrderDataSet.xsd&quot; xmlns:mstns=&quot;http://www.tempuri.org/OrderDataSet.xsd&quot; xmlns=&quot;http://www.tempuri.org/OrderDataSet.xsd&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:msdata=&quot;urn:schemas-microsoft-com:xml-msdata&quot; attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;qualified&quot;> <xs:element name=&quot;OrderDataSet&quot; msdata:IsDataSet=&quot;true&quot;> <xs:complexType> <xs:choice maxOccurs=&quot;unbounded&quot;> <xs:element name=&quot;Order_x0020_Details&quot;> <xs:complexType> <xs:sequence> <xs:element name=&quot;OrderID&quot; type=&quot;xs:int&quot; /> <xs:element name=&quot;ProductID&quot; type=&quot;xs:int&quot; /> <xs:element name=&quot;UnitPrice&quot; type=&quot;xs:decimal&quot; /> <xs:element name=&quot;Quantity&quot; type=&quot;xs:short&quot; /> <xs:element name=&quot;Discount&quot; type=&quot;xs:float&quot; /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name=&quot;Orders&quot;> <xs:complexType> <xs:sequence> <xs:element name=&quot;OrderID&quot; msdata:ReadOnly=&quot;true&quot; msdata:AutoIncrement=&quot;true&quot; type=&quot;xs:int&quot; /> <xs:element name=&quot;CustomerID&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;EmployeeID&quot; type=&quot;xs:int&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;OrderDate&quot; type=&quot;xs:dateTime&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;RequiredDate&quot; type=&quot;xs:dateTime&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShippedDate&quot; type=&quot;xs:dateTime&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipVia&quot; type=&quot;xs:int&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;Freight&quot; type=&quot;xs:decimal&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipName&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipAddress&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipCity&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipRegion&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipPostalCode&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;ShipCountry&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:unique name=&quot;Constraint1&quot; msdata:PrimaryKey=&quot;true&quot;> <xs:selector xpath=&quot;.//mstns:Order_x0020_Details&quot; /> <xs:field xpath=&quot;mstns:OrderID&quot; /> <xs:field xpath=&quot;mstns:ProductID&quot; /> </xs:unique> <xs:unique name=&quot;Orders_Constraint1&quot; msdata:ConstraintName=&quot;Constraint1&quot; msdata:PrimaryKey=&quot;true&quot;> <xs:selector xpath=&quot;.//mstns:Orders&quot; /> <xs:field xpath=&quot;mstns:OrderID&quot; /> </xs:unique> </xs:element> </xs:schema> </li> <li>On the Debug menu, click Start to run the application. The MainForm form appears.</li> <li>Click ViewOrder. The Form1 form, which contains two DataGrid controls, appears. The DataGrid controls are populated with data from your database.</li> <li>Modify the data of three cells in the second DataGrid control.</li> <li>Close the Form1 form.

Note Before you close the Form1 form, make sure that the focus is on the cell that you modified last.</li> <li>On the MainForm form, click View Order.

Notice that the cell that you modified last does not contain the modified data. Additionally, notice that the other cells contain the modified data.</li></ol>

<div class="references_section">