<%@ Page Language="VB" %>
<%@ Register TagPrefix="Ajax" Namespace="Ajax" Assembly="Ajax" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Private mcon As OleDbConnection
    Private mda As OleDbDataAdapter
    Private mcb As OleDbCommandBuilder
    Private mdvCustomers As DataView
    
    Sub Page_Load(sender As Object, e As EventArgs)
      Dim strSQL As String = "Select Top 10 * From Customers"
      Dim strConnectionString As String = _
        String.Format("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source={0}", _
          Server.MapPath("webdb/Nwind.mdb"))
    
      mcon = New OleDbConnection(strConnectionString)
      mda = New OleDbDataAdapter(strSQL, mcon)
      mcb = New OleDbCommandBuilder(mda)
    
      If Not IsPostBack Then
        BindGrid()
      Else
        mdvCustomers = CType(Session("Customers"), DataView)
      End If
    End Sub
    
    Sub AjaxGrid1_SortCommand(sender As Object, e As DataGridSortCommandEventArgs)
      If mdvCustomers.Sort <> e.SortExpression Then
        mdvCustomers.Sort = e.SortExpression
      Else
        mdvCustomers.Sort &= " DESC"
      End If
      BindGrid()
    End Sub
    
    Sub AjaxGrid1_EditCommand(sender As Object, e As DataGridCommandEventArgs)
      AjaxGrid1.EditItemIndex = e.Item.ItemIndex
      BindGrid()
    End Sub
    
    Sub AjaxGrid1_UpdateCommand(sender As Object, e As DataGridCommandEventArgs)
      Dim strCompanyName As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
      Dim strContactName As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
      Dim strPhone As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
      Dim intCustomerID As Integer = AjaxGrid1.DataKeys(e.Item.ItemIndex)
      Dim dt As DataTable = mdvCustomers.Table
      Dim dr As DataRow = dt.Rows.Find(intCustomerID)
    
      If Not dr Is Nothing Then
        dr("CompanyName") = strCompanyName
        dr("ContactName") = strContactName
        dr("Phone") = strPhone
        mda.Update(dt)
      End If
      AjaxGrid1.EditItemIndex = -1
      BindGrid()
    End Sub
    
    Sub AjaxGrid1_CancelCommand(sender As Object, e As DataGridCommandEventArgs)
      AjaxGrid1.EditItemIndex = -1
      BindGrid()
    End Sub
    
    Sub AjaxGrid1_DeleteCommand(sender As Object, e As DataGridCommandEventArgs)
      Dim intCustomerID As Integer = AjaxGrid1.DataKeys(e.Item.ItemIndex)
      Dim dt As DataTable = mdvCustomers.Table
      Dim dr As DataRow = dt.Rows.Find(intCustomerID)
    
      If Not (dr Is Nothing) Then
        dr.Delete
        mda.Update(dt)
      End If
      If AjaxGrid1.EditItemIndex = e.Item.ItemIndex Then
        AjaxGrid1.EditItemIndex = -1
      End If
      BindGrid()
    End Sub
    
    Sub BindGrid()
      If mdvCustomers Is Nothing Then
        mdvCustomers = CreateDataView()
        Session("Customers") = mdvCustomers
      End If
      AjaxGrid1.DataKeyField = "CustomerID"
      AjaxGrid1.DataSource = mdvCustomers
      AjaxGrid1.DataBind()
    End Sub
    
    Function CreateDataView() As DataView
      Dim dt As New DataTable()
      mda.Fill(dt)
      dt.PrimaryKey = New DataColumn() {dt.Columns("CustomerID")}
      Return New DataView(dt)
    End Function

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <h3>Ajax3 (<%= DateTime.Now %>)</h3>
        <AJAX:GRID id="AjaxGrid1" runat="server" OnSortCommand="AjaxGrid1_SortCommand" OnDeleteCommand="AjaxGrid1_DeleteCommand" OnCancelCommand="AjaxGrid1_CancelCommand" OnUpdateCommand="AjaxGrid1_UpdateCommand" OnEditCommand="AjaxGrid1_EditCommand" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC" AutoGenerateColumns="False" AllowSorting="True">
            <FOOTERSTYLE forecolor="#003399" backcolor="#99CCCC"></FOOTERSTYLE>
            <SELECTEDITEMSTYLE forecolor="#CCFF99" backcolor="#009999" font-bold="True"></SELECTEDITEMSTYLE>
            <ITEMSTYLE forecolor="#003399" backcolor="White"></ITEMSTYLE>
            <HEADERSTYLE forecolor="#CCCCFF" backcolor="#003399" font-bold="True"></HEADERSTYLE>
            <COLUMNS>
                <asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
                <asp:BoundColumn DataField="CompanyName" SortExpression="CompanyKana" HeaderText="得意先"></asp:BoundColumn>
                <asp:BoundColumn DataField="ContactName" SortExpression="ContactName" HeaderText="担当"></asp:BoundColumn>
                <asp:BoundColumn DataField="Phone" SortExpression="Phone" HeaderText="電話"></asp:BoundColumn>
                <asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="キャンセル" EditText="編集"></asp:EditCommandColumn>
                <asp:ButtonColumn ButtonType="PushButton" Text="削除" CommandName="Delete"></asp:ButtonColumn>
            </COLUMNS>
            <PAGERSTYLE forecolor="#003399" backcolor="#99CCCC" horizontalalign="Left" mode="NumericPages"></PAGERSTYLE>
        </AJAX:GRID>
    </form>
</body>
</html>
