using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using APNSoft.WebControls;

public partial class DataGrid_RowCellEditing : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Apply event-handling method
        myDataGrid.CellValueChanged += new DataGridEventHandler(myDataGrid_CellValueChanged);


        //Define SQL query
        string SqlQuery = @"SELECT CustomerID, CompanyName, ContactName, Address, PostalCode " + 
            "FROM Customers ORDER BY CustomerID";

        //Get DataTable (MS Access Database)
        DataTable myDataSource = DataBase.GetDataTableOleDb(SqlQuery, "~/DataGrid/DataBases/Nwind.mdb");

        //Set the data source
        myDataGrid.KeyFieldName = "CustomerID";
        myDataGrid.DataSource = myDataSource;
        myDataGrid.DataBind();


        //Disable Cell Editing for the CustomerID
        myDataGrid.Columns["CustomerID"].CellEditingEnabled = false;

    }


    //Server-side procedure for Cell Update
    void myDataGrid_CellValueChanged(object sender, DataGridEventArgs e)
    {
        //Get Grid
        APNSoftDataGrid myGrid = (APNSoftDataGrid)sender;

        //Get Row
        GridRow myGridRow = e.GridRow;
        if (myGridRow == null) return;

        //Get cell data
        GridColumn myGridColumn = e.GridColumn;
        string CellValue = e.CellValue;


        //Declare db objects
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
            Server.MapPath("~/DataGrid/DataBases/Nwind.mdb");

        //Create SQL Query
        string SQL = "UPDATE Customers SET " + myGridColumn.ColumnName +
            " = @Column WHERE " + myGrid.KeyFieldName + " = @RowID;";

        //Create command
        OleDbCommand cmd = new OleDbCommand(SQL, conn);

        //Add parameters
        cmd.Parameters.Add("@Column", OleDbType.VarChar).Value = CellValue;
        cmd.Parameters.Add("@RowID", OleDbType.VarChar).Value = myGridRow.RowID;

        //Execute the query
        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }
    }

}