Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports System.Data.OleDb
Imports APNSoft.WebControls

Partial Public Class DataGrid_RowCellEditing
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        'Apply event-handling method
        AddHandler myDataGrid.CellValueChanged, AddressOf myDataGrid_CellValueChanged


        'Define SQL query
        Dim SqlQuery As String = "SELECT CustomerID, CompanyName, ContactName, Address, PostalCode " & _
            "FROM Customers ORDER BY CustomerID"

        'Get DataTable (MS Access Database)
        Dim myDataSource As DataTable = 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

    End Sub


    'Server-side procedure for Cell Update
    Private Sub myDataGrid_CellValueChanged(ByVal sender As Object, ByVal e As DataGridEventArgs)
        'Get Grid
        Dim myGrid As APNSoftDataGrid = CType(sender, APNSoftDataGrid)

        'Get Row
        Dim myGridRow As GridRow = e.GridRow
        If myGridRow Is Nothing Then
            Return
        End If

        'Get cell data
        Dim myGridColumn As GridColumn = e.GridColumn
        Dim CellValue As String = e.CellValue


        'Declare db objects
        Dim conn As New OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
            Server.MapPath("~/DataGrid/DataBases/Nwind.mdb")

        'Create SQL Query
        Dim SQL As String = "UPDATE Customers SET " & _
            myGridColumn.ColumnName & " = @Column WHERE " & _
            myGrid.KeyFieldName & " = @RowID;"

        'Create command
        Dim cmd As 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()
        End Try
    End Sub

End Class