unit U_UserDeptSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, Buttons, DB, ADODB;

type
  TfrmUserDeptSel = class(TForm)
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    Label1: TLabel;
    Label2: TLabel;
    btb_add: TBitBtn;
    btb_del: TBitBtn;
    btb_clear: TBitBtn;
    DataSourceSel: TDataSource;
    ADOQuerySel: TADOQuery;
    ADOQueryToSel: TADOQuery;
    DataSourceToSel: TDataSource;
    ADOQueryToSelframeName: TStringField;
    ADOQueryToSelframeNo: TStringField;
    ADOQuerySelframeNo: TStringField;
    ADOQuerySelframeName: TStringField;
    ADOQuerySelUserId: TStringField;
    ADOQueryCmd: TADOQuery;
    ADOConnection1: TADOConnection;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btb_addClick(Sender: TObject);
    procedure btb_delClick(Sender: TObject);
    procedure btb_clearClick(Sender: TObject);
  private
    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmUserDeptSel: TfrmUserDeptSel;

implementation
uses
  U_global;
{$R *.dfm}
//////////////////////////////////////////////
  //初始化表格数据
//////////////////////////////////////////////
procedure TfrmUserDeptSel.InitGrid();
begin
  //被选择
  With ADOQueryTosel Do
  begin
    close;
    parameters.ParamByName('pUserId').value:=gUserId;
    open;
  end;
  //已选择
  With ADOQuerySel Do
  begin
    close;
    parameters.ParamByName('pUserId').value:=guserId;
    open;
  end;

end;
procedure TfrmUserDeptSel.FormCreate(Sender: TObject);
begin
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
  InitGrid();  
end;

procedure TfrmUserDeptSel.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
  Action:=cafree;
end;

procedure TfrmUserDeptSel.btb_addClick(Sender: TObject);
begin
  with ADOQuerySel do
  begin
    append;
    fieldByName('UserId').Value :=gUserId;
    fieldByName('frameNo').Value :=trim(ADOQueryToSelframeNo.AsString);
    post;
  end;
  ADOQuerySel.Requery();
  ADOQueryToSel.Requery(); 
end;

procedure TfrmUserDeptSel.btb_delClick(Sender: TObject);
begin
  if ADOQuerySel.IsEmpty then exit;
  ADOQuerySel.Delete;
  ADOQueryToSel.Requery(); 
end;

procedure TfrmUserDeptSel.btb_clearClick(Sender: TObject);
begin
  if ADOQuerySel.IsEmpty  then  exit;
  if application.MessageBox('确定要清除已设置的部门吗？','提示信息',1)=2 then exit; 
  with ADOQueryCmd do
  begin
    close;
    sql.Clear ;
    sql.Add('delete yc_userDept');
    sql.Add('where userId='''+gUserId+'''');
    execSql;
  end;
end;

end.
