context: //
  // DO NOT REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  //
  // @Authors:
  //       peters
  //
  // Copyright 2004-2011 by OM International
  //
  // This file is part of OpenPetra.org.
  //
  // OpenPetra.org is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // OpenPetra.org is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with OpenPetra.org.  If not, see <http://www.gnu.org/licenses/>.
  //
  using System;
  using System.Data;
  using System.Windows.Forms;
  
  using Ict.Common;
  using Ict.Common.Controls;
  using Ict.Petra.Client.App.Core.RemoteObjects;
  using Ict.Petra.Client.App.Gui;
  using Ict.Petra.Client.CommonForms;
  using Ict.Petra.Client.MCommon;
  using Ict.Petra.Shared;
  using Ict.Petra.Shared.MPartner.Partner.Data;
  
  namespace Ict.Petra.Client.MPartner.Gui
  {
      /// <summary>
      /// Description of TFrmBankFindDialog.ManualCode.
      /// </summary>
      public partial class TFrmBankFindDialog
      {
          #region Properties
  
          /// <summary>
          /// DataTable displayed in the grid
          /// </summary>
          private DataTable FCriteriaData = new DataTable();
  
          private Int64 FBankPartnerKey;
  
          /// <summary>
          /// Partner Key of selected Bank
          /// </summary>
          public Int64 BankPartnerKey
          {
              get
              {
                  return FBankPartnerKey;
              }
          }
  
          /// <summary>
          /// Dataset
          /// </summary>
          public BankTDS MainDS
          {
              get
              {
                  return FMainDS;
              }
          }
  
          #endregion
  
          #region Setup
  
          private void InitializeManualCode()
          {
              // set status bar text for controls
              FPetraUtilsObject.SetStatusBarText(txtBranchName, Catalog.GetString("Enter a Bank name"));
              FPetraUtilsObject.SetStatusBarText(txtBranchCode, Catalog.GetString("Enter a Bank/Branch Code"));
              FPetraUtilsObject.SetStatusBarText(txtBicCode, Catalog.GetString("Enter a Bank's BIC/SWIFT Code"));
              FPetraUtilsObject.SetStatusBarText(txtCity, Catalog.GetString("Enter a Bank's City"));
              FPetraUtilsObject.SetStatusBarText(txtCountry, Catalog.GetString("Enter a Bank's Country"));
              FPetraUtilsObject.SetStatusBarText(btnClear, Catalog.GetString("Clear the filter text"));
              FPetraUtilsObject.SetStatusBarText(btnEdit, Catalog.GetString("Open the Partner Edit screen for the selected Partner"));
              FPetraUtilsObject.SetStatusBarText(btnAccept, Catalog.GetString("Accept selected Bank"));
              FPetraUtilsObject.SetStatusBarText(btnCancel, Catalog.GetString("Cancel selection"));
  
              // add additional event to chkbox
              chkShowInactive.CheckedChanged += new System.EventHandler(this.SelectRowInGrid);
  
              // set initial focus
              this.ActiveControl = txtBranchName;
  
              // catch enter on all controls, to move focus to the grid
              this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CatchEnterKey);
          }
  
          /// <summary>
          /// Load data into grid
          /// </summary>
          /// <param name="AFirstTime">True if being run for the first time</param>
          public void LoadDataGrid(bool AFirstTime)
          {
              // Only call data from server if the dataset is actually empty.
              // (A filled dataset is passed to this screen from the 'Finance Details' tab and 'Find By Bank Details'.)
              if ((FMainDS == null) || (FMainDS.PBank.Rows.Count == 0))
              {
                  FMainDS = new BankTDS();
                  FMainDS.Merge(TRemote.MPartner.Partner.WebConnectors.GetPBankRecords());
              }
  
              FCriteriaData.Clear();
  
              if (AFirstTime)
              {
                  // setup the grid on first run
                  SetupGrid();
              }
  
              // create a new row for each bank record
              foreach (BankTDSPBankRow BankRow in FMainDS.PBank.Rows)
              {
                  if (BankRow.PartnerKey >= 0)
                  {
                      DataRow NewBankRow = FCriteriaData.NewRow();
                      NewBankRow[PBankTable.GetPartnerKeyDBName()] = BankRow.PartnerKey;
                      NewBankRow[BankTDSPBankTable.GetCityDBName()] = BankRow.City;
                      NewBankRow[BankTDSPBankTable.GetCountryCodeDBName()] = BankRow.CountryCode;
                      NewBankRow[PBankTable.GetBranchNameDBName()] = BankRow.BranchName;
  
                      if (BankRow.BranchCode.StartsWith(SharedConstants.INACTIVE_VALUE_WITH_QUALIFIERS + " "))
                      {
                          NewBankRow[PBankTable.GetBranchCodeDBName()] = BankRow.BranchCode.Substring(11);
                      }
                      else if (BankRow.BranchCode.StartsWith(SharedConstants.INACTIVE_VALUE_WITH_QUALIFIERS))
                      {
                          NewBankRow[PBankTable.GetBranchCodeDBName()] = BankRow.BranchCode.Substring(10);
                      }
                      else
                      {
                          NewBankRow[PBankTable.GetBranchCodeDBName()] = BankRow.BranchCode;
                      }
  
                      NewBankRow[BankTDSPBankTable.GetSiteKeyDBName()] = BankRow.SiteKey;
                      NewBankRow[BankTDSPBankTable.GetLocationKeyDBName()] = BankRow.LocationKey;
  
                      NewBankRow[PBankTable.GetBicDBName()] = BankRow.Bic;
                      NewBankRow[BankTDSPBankTable.GetStatusCodeDBName()] = BankRow.StatusCode;
                      FCriteriaData.Rows.Add(NewBankRow);
                  }
              }
  
              // sort order for grid
              DataView MyDataView = FCriteriaData.DefaultView;
              MyDataView.Sort = "p_branch_name_c ASC";
              grdDetails.DataSource = new DevAge.ComponentModel.BoundDataView(MyDataView);
  
              SelectRowInGrid();
              UpdateRecordNumberDisplay();
              Filter(this, null);
          }
  
          private void SetupGrid()
          {
              // create a new table
              FCriteriaData.Columns.Add(PBankTable.GetPartnerKeyDBName(), Type.GetType("System.Int64"));
              FCriteriaData.Columns.Add(BankTDSPBankTable.GetSiteKeyDBName(), Type.GetType("System.Int64"));
              FCriteriaData.Columns.Add(BankTDSPBankTable.GetLocationKeyDBName(), Type.GetType("System.Int32"));
              FCriteriaData.Columns.Add(PBankTable.GetBranchNameDBName(), Type.GetType("System.String"));
              FCriteriaData.Columns.Add(PBankTable.GetBranchCodeDBName(), Type.GetType("System.String"));
              FCriteriaData.Columns.Add(PBankTable.GetBicDBName(), Type.GetType("System.String"));
              FCriteriaData.Columns.Add(BankTDSPBankTable.GetCityDBName(), Type.GetType("System.String"));
              FCriteriaData.Columns.Add(BankTDSPBankTable.GetCountryCodeDBName(), Type.GetType("System.String"));
              FCriteriaData.Columns.Add(BankTDSPBankTable.GetStatusCodeDBName(), Type.GetType("System.String"));
  
              FCriteriaData.PrimaryKey = new DataColumn[] {
                  FCriteriaData.Columns[PBankTable.GetPartnerKeyDBName()],
                  FCriteriaData.Columns[BankTDSPBankTable.GetSiteKeyDBName()], FCriteriaData.Columns[BankTDSPBankTable.GetLocationKeyDBName()]
              };
              FCriteriaData.DefaultView.AllowNew = false;
  
              // add columns to the grid
              grdDetails.Columns.Clear();
              grdDetails.AddTextColumn("Partner Key", FCriteriaData.Columns[PBankTable.GetPartnerKeyDBName()], 80);
              grdDetails.AddTextColumn("Bank Name", FCriteriaData.Columns[PBankTable.GetBranchNameDBName()]);
              grdDetails.AddTextColumn("Bank/Branch Code", FCriteriaData.Columns[PBankTable.GetBranchCodeDBName()], 120);
              grdDetails.AddTextColumn("BIC/SWIFT Code", FCriteriaData.Columns[PBankTable.GetBicDBName()], 110);
              grdDetails.AddTextColumn("Status", FCriteriaData.Columns[BankTDSPBankTable.GetStatusCodeDBName()], 70);
              grdDetails.AddTextColumn("City", FCriteriaData.Columns[PLocationTable.GetCityDBName()], 80);
              grdDetails.AddTextColumn("Country", FCriteriaData.Columns[PLocationTable.GetCountryCodeDBName()], 60);
  
              grdDetails.DoubleClickCell += new TDoubleClickCellEventHandler(grdDetails_DoubleClickCell);
              grdDetails.EnterKeyPressed += new TKeyPressedEventHandler(grdDetails_EnterKey);
              grdDetails.Selection.FocusRowEntered += new SourceGrid.RowEventHandler(this.FocusedRowChanged);
          }
  
          #endregion
  
          #region Helper methods
  
          // automatically select the currently selected Bank (if it exists)
          private void SelectRowInGrid()
          {
              int RowPos = 1;
  
              // if no bank is selected then no row should be selected
              if ((grdDetails.Rows.Count <= 1) || (FBankPartnerKey == 0))
              {
                  // select the first row in the grid
  
gt:                 if (grdDetails.Rows.Count > 1)