unit GridAsync;
{PUBDIST}

interface

uses
  IWAppForm, IWApplication, IWTypes, Classes, Controls, IWControl,
  IWWebGrid, IWAdvWebGrid, IWCompButton, IWCompListbox, SysUtils,
  IWCompLabel, ImgList, IWBaseControl, LeftFrame, Forms, TopFrame,
  IWVCLBaseControl, IWBaseHTMLControl, Windows, IWCompText;

type
  TformGridAsync = class(TIWAppForm)
    FrameTopFrame1: TFrameTopFrame;                  
    FrameLeftFrame1: TFrameLeftFrame;
    IWLabel2: TIWLabel;
    TIWAdvWebGrid1: TTIWAdvWebGrid;
    IWText1: TIWText;
    IWButtonPresets: TIWButton;
    IWButtonEdit: TIWButton;
    IWButtonPost: TIWButton;
    IWButtonCancel: TIWButton;
    procedure IWAppFormCreate(Sender: TObject);
    procedure TIWAdvWebGrid1AsyncPost(Sender: TObject;
      EventParams: TStringList);
    procedure IWButton1Click(Sender: TObject);
    procedure IWButtonPresetsAsyncClick(Sender: TObject; EventParams: TStringList);
    procedure IWButtonEditAsyncClick(Sender: TObject; EventParams: TStringList);
    procedure IWButtonPostAsyncClick(Sender: TObject; EventParams: TStringList);
    procedure IWButtonCancelAsyncClick(Sender: TObject;
      EventParams: TStringList);
    procedure SetBrowseMode();
    procedure SetEditMode();
    procedure PostData();
    procedure TIWAdvWebGrid1AsyncEdit(Sender: TObject;
      EventParams: TStringList);
    procedure TIWAdvWebGrid1AsyncCancel(Sender: TObject;
      EventParams: TStringList);
    procedure TIWAdvWebGrid1AsyncPostValidate(Sender: TObject;
      PostData: TStringList; var Allow, EditMode: Boolean;
      var AlertMessage: string);
  public
  CountryCol, EditedRow: integer;
  end;

implementation
{$R *.dfm}

uses
  ServerController;

procedure TformGridAsync.SetBrowseMode();
begin
  IWButtonEdit.Enabled := true;
  IWButtonPost.Enabled := false;
  IWButtonCancel.Enabled := false;
end;

procedure TformGridAsync.SetEditMode();
begin
  IWButtonEdit.Enabled := false;
  IWButtonPost.Enabled := true;
  IWButtonCancel.Enabled := true;
end;

procedure TformGridAsync.PostData();
var
  Company, Capital, CellValue: string;
begin
  SetBrowseMode;
  CellValue := TIWAdvWebGrid1.Cells[CountryCol,EditedRow]; 

  if CellValue = 'Belgium' then
  begin
    Capital := 'Brussels';
    Company := 'SN';
  end
  else if CellValue = 'France' then
  begin
    Capital := 'Paris';
    Company := 'Air France';
  end
  else if CellValue = 'Germany' then
  begin
    Capital := 'Berlin';
    Company := 'Lufthansa';
  end
  else if CellValue = 'Netherlands' then
  begin
    Capital := 'Amsterdam';
    Company := 'KLM';
  end
  else if CellValue = 'United Kingdom' then
  begin
    Capital := 'London';
    Company := 'Brittish Airways';
  end;

  TIWAdvWebGrid1.AsyncSetCell(3, EditedRow, Capital);
  TIWAdvWebGrid1.AsyncSetCell(4, EditedRow, Company);
end;

procedure TformGridAsync.IWAppFormCreate(Sender: TObject);
begin

  CountryCol := 2;

  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('');
  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('Belgium');
  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('France');
  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('Germany');
  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('Netherlands');
  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('United Kingdom');
  TIWAdvWebGrid1.Columns[CountryCol].ComboItems.Add('Invalid');
end;

procedure TformGridAsync.IWButton1Click(Sender: TObject);
begin
  TIWAdvWebGrid1.Page := 0;

  TIWAdvWebGrid1.Cells[2,0] := 'United Kingdom';
  TIWAdvWebGrid1.Cells[3,0] := 'London';
  TIWAdvWebGrid1.Cells[4,0] := 'British Airways';
  TIWAdvWebGrid1.Cells[5,0] := 'John';
  TIWAdvWebGrid1.Cells[6,0] := '14/11/2007';

  TIWAdvWebGrid1.Cells[2,1] := 'Germany';
  TIWAdvWebGrid1.Cells[3,1] := 'Berlin';
  TIWAdvWebGrid1.Cells[4,1] := 'Lufthansa';
  TIWAdvWebGrid1.Cells[5,1] := 'Hans';
  TIWAdvWebGrid1.Cells[6,1] := '19/11/2007';
end;

procedure TformGridAsync.IWButtonCancelAsyncClick(Sender: TObject;
  EventParams: TStringList);
begin
  SetBrowseMode;
  TIWAdvWebGrid1.AsyncDoCancel;
end;

procedure TformGridAsync.IWButtonPostAsyncClick(Sender: TObject;
  EventParams: TStringList);
begin
  if  TIWAdvWebGrid1.AsyncDoPost then
  begin
    PostData;
  end;       
end;

procedure TformGridAsync.IWButtonPresetsAsyncClick(Sender: TObject;
  EventParams: TStringList);
begin
  TIWAdvWebGrid1.AsyncSetCell(2,0,'United Kingdom');
  TIWAdvWebGrid1.AsyncSetCell(3,0,'London');
  TIWAdvWebGrid1.AsyncSetCell(4,0,'British Airways');
  TIWAdvWebGrid1.AsyncSetCell(5,0,'John');
  TIWAdvWebGrid1.AsyncSetCell(6,0,'14/11/2007');

  TIWAdvWebGrid1.AsyncSetCell(2,1,'Germany');
  TIWAdvWebGrid1.AsyncSetCell(3,1,'Berlin');
  TIWAdvWebGrid1.AsyncSetCell(4,1,'Lufthansa');
  TIWAdvWebGrid1.AsyncSetCell(5,1,'Hans');
  TIWAdvWebGrid1.AsyncSetCell(6,1,'19/11/2007');
end;

procedure TformGridAsync.IWButtonEditAsyncClick(Sender: TObject;
  EventParams: TStringList);
begin
  SetEditMode;
  EditedRow := 1;
  TIWAdvWebGrid1.AsyncSetEditMode(EditedRow);
end;

procedure TformGridAsync.TIWAdvWebGrid1AsyncCancel(Sender: TObject;
  EventParams: TStringList);
begin
  SetBrowseMode;
end;

procedure TformGridAsync.TIWAdvWebGrid1AsyncEdit(Sender: TObject;
  EventParams: TStringList);
begin
  EditedRow := TIWAdvWebGrid1.ActiveRow;
  SetEditMode;
end;

procedure TformGridAsync.TIWAdvWebGrid1AsyncPost(Sender: TObject;
  EventParams: TStringList);
begin
  PostData;
end;

procedure TformGridAsync.TIWAdvWebGrid1AsyncPostValidate(Sender: TObject;
  PostData: TStringList; var Allow, EditMode: Boolean;
  var AlertMessage: string);
begin
  if PostData.IndexOf('Invalid') > 0 then
  begin
    Allow := false;
    EditMode := true;
    AlertMessage := 'The selected item is invalid, please select another item.';
  end;

  if not EditMode then  
    SetBrowseMode;
end;

initialization
  TformGridAsync.SetAsMainForm;

end.
