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_ColumnsTemplateDropDowns : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //Apply event-handling method
        myDataGrid.CallTheServer += new APNSoft.WebControls.DataGridEventHandler(myDataGrid_CallTheServer);

        //Create new DataTable
        string SqlQuery = @"SELECT ProductID, ProductName, CategoryID FROM Products ORDER BY ProductID";

        //Set the data source
        myDataGrid.KeyFieldName = "ProductID";
        myDataGrid.DataSource = DataBase.GetDataTableOleDb(SqlQuery, "~/DataGrid/DataBases/Nwind.mdb");
        myDataGrid.DataBind();

        myDataGrid.Columns["CategoryID"].Template = "~/DataGrid/Templates/Categories.ascx";
    }


    //Server-side procedure for Update
    private void myDataGrid_CallTheServer(object sender, APNSoft.WebControls.DataGridEventArgs e)
    {

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

        //Get cell value
        int CellValue = int.Parse(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 Products SET CategoryID=@CategoryID WHERE ProductID=@ProductID";

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

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

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

    }

}