Microsoft KB Archive/833032

From BetaArchive Wiki

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 "Symptoms" section.

WORKAROUND

To work around this bug, follow these steps:

  1. Start Microsoft Visual Studio .NET
  2. Open the form that contains the DataGrid control.
  3. 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 "Applies to" section of this article.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C# Projects under Project Types, and then click Windows Application under Templates.
  4. In the Location text box, type C:\MyFolder.

    NoteMyFolder is a placeholder for the name of the folder where you want to save your application.
  5. In the Name text box, type GridApplication1 as the name of your application.
  6. Click OK. By default, the Form1.cs file is created.
  7. Right-click Form1, and then click View Code.
  8. 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>
        /// Summary description for Form1.
        /// </summary>
        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;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
    
            public Form1()
            {
                //
                // Required for Windows Form Designer support
                //
                InitializeComponent();
    
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            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
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            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("Table", "Orders", new System.Data.Common.DataColumnMapping[] 
          {
        new System.Data.Common.DataColumnMapping("OrderID", "OrderID"),
        new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
        new System.Data.Common.DataColumnMapping("EmployeeID", "EmployeeID"),
        new System.Data.Common.DataColumnMapping("OrderDate", "OrderDate"),
        new System.Data.Common.DataColumnMapping("RequiredDate", "RequiredDate"),
        new System.Data.Common.DataColumnMapping("ShippedDate", "ShippedDate"),
        new System.Data.Common.DataColumnMapping("ShipVia", "ShipVia"),
        new System.Data.Common.DataColumnMapping("Freight", "Freight"),
        new System.Data.Common.DataColumnMapping("ShipName", "ShipName"),
        new System.Data.Common.DataColumnMapping("ShipAddress", "ShipAddress"),
        new System.Data.Common.DataColumnMapping("ShipCity", "ShipCity"),
        new System.Data.Common.DataColumnMapping("ShipRegion", "ShipRegion"),
        new System.Data.Common.DataColumnMapping("ShipPostalCode", "ShipPostalCode"),
        new System.Data.Common.DataColumnMapping("ShipCountry", "ShipCountry")})});
        this.daOrder.UpdateCommand = this.oleDbUpdateCommand1;
                
                // 
                // oleDbConnection1
                // 
        this.oleDbConnection1.ConnectionString = 
            @"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";
    
    
        // 
        // oleDbSelectCommand1
        // 
        this.oleDbSelectCommand1.CommandText = 
            "SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, Shi" +
            "pVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, Ship" +
            "Country FROM Orders";
        this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
                // 
                // oleDbUpdateCommand1
                // 
        this.oleDbUpdateCommand1.CommandText = 
            @"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 = ?)";
        this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("CustomerID", System.Data.OleDb.OleDbType.VarWChar, 5, "CustomerID"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("EmployeeID", System.Data.OleDb.OleDbType.Integer, 4, "EmployeeID"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("OrderDate", System.Data.OleDb.OleDbType.DBTimeStamp, 8, "OrderDate"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("RequiredDate", System.Data.OleDb.OleDbType.DBTimeStamp, 8, 
            "RequiredDate"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShippedDate", System.Data.OleDb.OleDbType.DBTimeStamp, 
            8, "ShippedDate"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipVia", System.Data.OleDb.OleDbType.Integer, 4, "ShipVia"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Freight", System.Data.OleDb.OleDbType.Currency, 8, "Freight"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipName", System.Data.OleDb.OleDbType.VarWChar, 40, "ShipName"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipAddress", System.Data.OleDb.OleDbType.VarWChar, 
            60, "ShipAddress"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipCity", System.Data.OleDb.OleDbType.VarWChar, 15, "ShipCity"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipRegion", System.Data.OleDb.OleDbType.VarWChar, 15, 
            "ShipRegion"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipPostalCode", System.Data.OleDb.OleDbType.VarWChar, 10, 
            "ShipPostalCode"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ShipCountry", System.Data.OleDb.OleDbType.VarWChar, 15, 
            "ShipCountry"));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_OrderID", System.Data.OleDb.OleDbType.Integer, 4, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "OrderID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter
            ("Original_CustomerID", System.Data.OleDb.OleDbType.VarWChar, 5, System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), 
            ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_CustomerID1", System.Data.OleDb.OleDbType.VarWChar, 5, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_EmployeeID", System.Data.OleDb.OleDbType.Integer, 4, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_EmployeeID1", System.Data.OleDb.OleDbType.Integer, 4, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_Freight", System.Data.OleDb.OleDbType.Currency, 8, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "Freight", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_Freight1", System.Data.OleDb.OleDbType.Currency, 8, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "Freight", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_OrderDate", System.Data.OleDb.OleDbType.DBTimeStamp, 8, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "OrderDate", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter
            ("Original_OrderDate1", System.Data.OleDb.OleDbType.DBTimeStamp, 8, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "OrderDate", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_RequiredDate", System.Data.OleDb.OleDbType.DBTimeStamp, 
            8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "RequiredDate", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_RequiredDate1", System.Data.OleDb.OleDbType.DBTimeStamp, 
            8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "RequiredDate", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipAddress", 
            System.Data.OleDb.OleDbType.VarWChar, 60, System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "ShipAddress", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipAddress1", System.Data.OleDb.OleDbType.VarWChar, 60, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "ShipAddress", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipCity", System.Data.OleDb.OleDbType.VarWChar, 15, 
            System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "ShipCity", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipCity1", System.Data.OleDb.OleDbType.VarWChar, 15, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "ShipCity", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipCountry", System.Data.OleDb.OleDbType.VarWChar, 15, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipCountry", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipCountry1", System.Data.OleDb.OleDbType.VarWChar, 15, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "ShipCountry", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipName", System.Data.OleDb.OleDbType.VarWChar, 40, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipName", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipName1", System.Data.OleDb.OleDbType.VarWChar, 40, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipName", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter
            ("Original_ShipPostalCode", System.Data.OleDb.OleDbType.VarWChar, 10, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "ShipPostalCode", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipPostalCode1", System.Data.OleDb.OleDbType.VarWChar, 
            10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "ShipPostalCode", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipRegion", System.Data.OleDb.OleDbType.VarWChar, 
            15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "ShipRegion", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipRegion1", System.Data.OleDb.OleDbType.VarWChar, 
            15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipRegion", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipVia", System.Data.OleDb.OleDbType.Integer, 4, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipVia", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShipVia1", System.Data.OleDb.OleDbType.Integer, 
            4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipVia", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShippedDate", System.Data.OleDb.OleDbType.DBTimeStamp, 
            8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShippedDate", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ShippedDate1", System.Data.OleDb.OleDbType.DBTimeStamp, 
            8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShippedDate", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Select_OrderID", 
            System.Data.OleDb.OleDbType.Integer, 4, "OrderID"));
        // 
        // daOrderDetails
        // 
    
        this.daOrderDetails.SelectCommand = this.oleDbSelectCommand2;
        this.daOrderDetails.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] 
        {
            
        new System.Data.Common.DataTableMapping("Table", "Order Details", 
        new System.Data.Common.DataColumnMapping[] 
        {
        new System.Data.Common.DataColumnMapping("OrderID", "OrderID"),
        new System.Data.Common.DataColumnMapping("ProductID", "ProductID"),
        new System.Data.Common.DataColumnMapping("UnitPrice", "UnitPrice"),
        new System.Data.Common.DataColumnMapping("Quantity", "Quantity"),
        new System.Data.Common.DataColumnMapping("Discount", "Discount")})});
        this.daOrderDetails.UpdateCommand = this.oleDbUpdateCommand2;
    
    //          // 
        // oleDbSelectCommand2
        // 
        this.oleDbSelectCommand2.CommandText = 
            "SELECT OrderID, ProductID, UnitPrice, Quantity, Discount FROM [Order Details]";
        this.oleDbSelectCommand2.Connection = this.oleDbConnection1;
        // 
        // oleDbUpdateCommand2
        // 
        this.oleDbUpdateCommand2.CommandText = 
            @"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 = ?)";
            this.oleDbUpdateCommand2.Connection = this.oleDbConnection1;
            this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("OrderID", System.Data.OleDb.OleDbType.Integer, 4, "OrderID"));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("ProductID", System.Data.OleDb.OleDbType.Integer, 4, "ProductID"));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("UnitPrice", System.Data.OleDb.OleDbType.Currency, 8, "UnitPrice"));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Quantity", System.Data.OleDb.OleDbType.SmallInt, 2, "Quantity"));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Discount", System.Data.OleDb.OleDbType.Single, 4, "Discount"));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_OrderID", System.Data.OleDb.OleDbType.Integer, 4, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "OrderID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_ProductID", System.Data.OleDb.OleDbType.Integer, 4, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "ProductID", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_Discount", System.Data.OleDb.OleDbType.Single, 4, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Discount", 
            System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Original_Quantity", System.Data.OleDb.OleDbType.SmallInt, 2, 
            System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), 
            ((System.Byte)(0)), "Quantity", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter
            ("Original_UnitPrice", System.Data.OleDb.OleDbType.Currency, 8, System.Data.ParameterDirection.Input, false, 
            ((System.Byte)(0)), ((System.Byte)(0)), "UnitPrice", System.Data.DataRowVersion.Original, null));
        this.oleDbUpdateCommand2.Parameters.Add
            (new System.Data.OleDb.OleDbParameter("Select_OrderID", System.Data.OleDb.OleDbType.Integer, 4, "OrderID"));
        this.oleDbUpdateCommand2.Parameters.Add(new System.Data.OleDb.OleDbParameter
            ("Select_ProductID", System.Data.OleDb.OleDbType.Integer, 4, "ProductID"));
                // 
                // orderDataSet1
                // 
                this.orderDataSet1.DataSetName = "OrderDataSet";
                this.orderDataSet1.Locale = new System.Globalization.CultureInfo("en-US");
                // 
                // dataGrid1
                // 
                this.dataGrid1.DataMember = "";
                this.dataGrid1.DataSource = this.dvOrder;
                this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                this.dataGrid1.Location = new System.Drawing.Point(8, 16);
                this.dataGrid1.Name = "dataGrid1";
                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 = "Orders";
                this.dataGridTableStyle1.RowHeadersVisible = false;
                // 
                // dataGridTextBoxColumn1
                // 
                this.dataGridTextBoxColumn1.Format = "";
                this.dataGridTextBoxColumn1.FormatInfo = null;
                this.dataGridTextBoxColumn1.HeaderText = "OrderID";
                this.dataGridTextBoxColumn1.MappingName = "OrderID";
                this.dataGridTextBoxColumn1.ReadOnly = true;
                this.dataGridTextBoxColumn1.Width = 75;
                // 
                // dataGridTextBoxColumn2
                // 
                this.dataGridTextBoxColumn2.Format = "";
                this.dataGridTextBoxColumn2.FormatInfo = null;
                this.dataGridTextBoxColumn2.HeaderText = "Ship City";
                this.dataGridTextBoxColumn2.MappingName = "ShipCity";
                this.dataGridTextBoxColumn2.Width = 75;
                // 
                // dataGrid2
                // 
                this.dataGrid2.DataMember = "";
                this.dataGrid2.DataSource = this.dvOrderDetails;
                this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                this.dataGrid2.Location = new System.Drawing.Point(312, 16);
                this.dataGrid2.Name = "dataGrid2";
                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 = "Order Details";
                this.dataGridTableStyle2.RowHeadersVisible = false;
                // 
                // dataGridTextBoxColumn3
                // 
                this.dataGridTextBoxColumn3.Format = "";
                this.dataGridTextBoxColumn3.FormatInfo = null;
                this.dataGridTextBoxColumn3.HeaderText = "Product ID";
                this.dataGridTextBoxColumn3.MappingName = "ProductID";
                this.dataGridTextBoxColumn3.ReadOnly = true;
                this.dataGridTextBoxColumn3.Width = 75;
                // 
                // dataGridTextBoxColumn4
                // 
                this.dataGridTextBoxColumn4.Format = "";
                this.dataGridTextBoxColumn4.FormatInfo = null;
                this.dataGridTextBoxColumn4.HeaderText = "Quantity";
                this.dataGridTextBoxColumn4.MappingName = "Quantity";
                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 = "Form1";
                this.Text = "Form1";
                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
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [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["OrderID"].ToString();
                    this.dvOrderDetails.RowFilter = " OrderID = " + 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();
            }
    
    
        }
    }
  9. Add a form that is named MainForm to the project. To do this, follow these steps:
    1. In Solution Explorer, right-click GridApplication1.
    2. Point to Add, and then click Add New Item. The Add New Item - GridApplication1 dialog box appears.
    3. Under Templates, click Windows Form.
    4. In the Name text box, type MainForm.cs.
    5. Click Open.
  10. Right-click MainForm, and then click View Code.
  11. 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>
        /// Summary description for MainForm.
        /// </summary>
        public class MainForm : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Button button1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
    
            public MainForm()
            {
                //
                // Required for Windows Form Designer support
                //
                InitializeComponent();
    
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if(components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
    
        #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            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 = "button1";
                this.button1.TabIndex = 0;
                this.button1.Text = "View Orders";
                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 = "MainForm";
                this.Text = "MainForm";
                this.ResumeLayout(false);
    
            }
        #endregion
    
            private void button1_Click(object sender, System.EventArgs e)
            {
                GridApplication1.Form1 OrderForm = new GridApplication1.Form1();
                OrderForm.Show();
            }
        }
    }
    
    
  12. Add a DataSet control to the project. To add a DataSet control, follow these steps:
    1. In Solution Explorer, right-click GridApplication1.
    2. Point to Add, and then click Add New Item. The Add New Item - GridApplication1 dialog box appears.
    3. Under Templates, click Data Set.
    4. In the Name text box, type OrderDataSet.xsd.
    5. Click Open.
  13. Switch to the XML view of the OrderDataSet data set.
  14. In the OrderDataSet.xsd file, replace the existing code with the following code:

    <?xml version="1.0" standalone="yes" ?>
    <xs:schema id="OrderDataSet" targetNamespace="http://www.tempuri.org/OrderDataSet.xsd" 
    xmlns:mstns="http://www.tempuri.org/OrderDataSet.xsd"
    xmlns="http://www.tempuri.org/OrderDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" 
    elementFormDefault="qualified">
    <xs:element name="OrderDataSet" msdata:IsDataSet="true">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
    <xs:element name="Order_x0020_Details">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="OrderID" type="xs:int" />
    <xs:element name="ProductID" type="xs:int" />
    <xs:element name="UnitPrice" type="xs:decimal" />
    <xs:element name="Quantity" type="xs:short" />
    <xs:element name="Discount" type="xs:float" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="Orders">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="OrderID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" />
    <xs:element name="CustomerID" type="xs:string" minOccurs="0" />
    <xs:element name="EmployeeID" type="xs:int" minOccurs="0" />
    <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />
    <xs:element name="RequiredDate" type="xs:dateTime" minOccurs="0" />
    <xs:element name="ShippedDate" type="xs:dateTime" minOccurs="0" />
    <xs:element name="ShipVia" type="xs:int" minOccurs="0" />
    <xs:element name="Freight" type="xs:decimal" minOccurs="0" />
    <xs:element name="ShipName" type="xs:string" minOccurs="0" />
    <xs:element name="ShipAddress" type="xs:string" minOccurs="0" />
    <xs:element name="ShipCity" type="xs:string" minOccurs="0" />
    <xs:element name="ShipRegion" type="xs:string" minOccurs="0" />
    <xs:element name="ShipPostalCode" type="xs:string" minOccurs="0" />
    <xs:element name="ShipCountry" type="xs:string" minOccurs="0" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
    <xs:selector xpath=".//mstns:Order_x0020_Details" />
    <xs:field xpath="mstns:OrderID" />
    <xs:field xpath="mstns:ProductID" />
    </xs:unique>
    <xs:unique name="Orders_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
    <xs:selector xpath=".//mstns:Orders" />
    <xs:field xpath="mstns:OrderID" />
    </xs:unique>
    </xs:element>
    </xs:schema>
  15. On the Debug menu, click Start to run the application. The MainForm form appears.
  16. Click ViewOrder. The Form1 form, which contains two DataGrid controls, appears. The DataGrid controls are populated with data from your database.
  17. Modify the data of three cells in the second DataGrid control.
  18. Close the Form1 form.

    Note Before you close the Form1 form, make sure that the focus is on the cell that you modified last.
  19. 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.


REFERENCES

For additional information about DataGrid objects, DataGrid in-place editing, and form events, visit the following Microsoft Web sites:

Keywords: kbvs2002sp1sweep kbwindowsforms kbdataadapter kbbug KB833032